{"id":4033,"date":"2022-01-25T12:02:13","date_gmt":"2022-01-25T12:02:13","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=4033"},"modified":"2022-02-17T07:13:46","modified_gmt":"2022-02-17T07:13:46","slug":"using-regex-in-php","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/regex-regular-expressions\/using-regex-in-php","title":{"rendered":"Using regex in PHP"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">preg_match()<\/h4>\n\n\n\n<p>Does input string match regex expression<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $InputString = \"abcd\";\n  $Length = strlen($InputString);\n  $RegexPattern = '^&#91;(]*(&#91;0-9]{3})&#91;- .)]*&#91;0-9]{3}&#91;- .]*&#91;0-9]{4}$';\n  if (\n      ($Length &gt; 2) &amp;&amp; ($Length &lt; 100) &amp;&amp;    \/\/Testing for length first protects against malicious very long inputs\n      (preg_match($RegexPattern, InputString))   \/\/Returns 1 if regex matches\n  {\n\n \n\n  } <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">preg_replace()<\/h4>\n\n\n\n<p>Replace any characters that don&#8217;t match the regex expression with character from another string.<\/p>\n\n\n\n<p>You can make the replacement string empty to simply remove unwanted characters<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Remove all characters that are not numbers example<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $InputString = \"01-23(4)\";\n  $RegexPattern = \"&#91;^0-9]\";\n  $FormattedNumber = preg_replace($RegexPattern, \"\", $InputString)\n  \/\/$FormattedNumber = \"01234\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>preg_match() Does input string match regex expression preg_replace() Replace any characters that don&#8217;t match the regex expression with character from another string. You can make the replacement string empty to simply remove unwanted characters Remove all characters that are not numbers example<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-4033","post","type-post","status-publish","format-standard","hentry","category-regex-regular-expressions"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=4033"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4033\/revisions"}],"predecessor-version":[{"id":4038,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4033\/revisions\/4038"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=4033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=4033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=4033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}