{"id":561,"date":"2010-01-22T12:17:24","date_gmt":"2010-01-22T12:17:24","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=561"},"modified":"2010-01-22T12:19:37","modified_gmt":"2010-01-22T12:19:37","slug":"regular-expression-php","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/22\/regular-expression-php\/","title":{"rendered":"Regular expression &#8211; PHP"},"content":{"rendered":"<p><a href=\"http:\/\/uk3.php.net\/preg_match_all\"><a href=\"http:\/\/en.wikipedia.org\/wiki\/Regular_expression\">Regular expression<\/a> is when you have a match condition and a string to search through.  Here is a <a href=\"http:\/\/www.addedbytes.com\/download\/regular-expressions-cheat-sheet-v2\/pdf\/\">cheat sheet<\/a> to download to view regular expressions syntax.<\/p>\n<p>The regular matching method in php that I use is preg_match_all<\/p>\n<pre lang=\"php\">\r\nint preg_match_all ( string pattern, string subject, array &matches [, int flags [, int offset]] )\r\n<\/pre>\n<p>which basically means that it will return a integer value of how many matches it has found within the subject text.  The flags and offset are optional values.<\/p>\n<p>Here is a example to pull out HTML A links within a text, A links start with &lt;a and have some optional parameters e.g. href, title &gt;text to display on the screen&lt;\/a&gt; (the closing tag.  So in xml speak as such<\/p>\n<pre lang=\"xml\">\r\n<a[optional attributes]>value<\/a>\r\n<\/pre>\n<p>So the basics of searching for HTML A links within a string is to look for &#8220;<a\" and then 0-x amount of characters that ends with \">&#8221; which then has the value and then the closing tag <\/a>.  So to put that in regular expression talk, to break it down we first search for the start <strong>&lt;a<\/strong> followed by x amount of characters <strong>\\s.*<\/strong> with a ending tag of <strong><\\\/a><\/strong> the &#8220;\\&#8221; is there because the \/ is a condition statement for regular expression.  And with this together it would look like<\/p>\n<pre lang=\"php\">\r\n<a\\s.*<\\\/a>\r\n<\/pre>\n<p>But because <\\\/a> happens twice in the string and either one can be the end point of the search since we are searching string values &#8220;\\s.*&#8221; 0-x amount of them.  \\s means white space character &#8220;.&#8221; means any character that is not \\n and &#8220;*&#8221; means 0 &#8211; more times of the previous check.  So there is potential for a problem here we are not telling the regular expression to stop at the first < character and to this we use the [] to match some characters, the ^ in this context means any character not in this list about to come and \"<\" is the stopping character.  So the full regular expression will be.\n\n\n\n<pre lang=\"php\">\r\n<a\\s.[^<]*<\\\/a>\r\n<\/pre>\n<p>Here is the php code that will display all of the HTML A links from a string.<\/p>\n<pre lang=\"php\">\r\n<?php\r\n\/\/ here is a string with 2 a href links inside it and sounding text.\r\n$matchAStr = \"coding friends is my site :) <a href=\\\"http:\/\/www.codingfriends.com\\\">codingfriends.com<\/a> hi there.. \" . \r\n\t    \" mountains are nice. <a href=\\\"http:\/\/www.norfolkhospice.org.uk\/\\\">norfolkhospice.org.uk<\/a>\" . \r\n\t    \" support the tapping house in Norfolk\";\r\n\r\n\/\/ the preg_match_all uses regular expression to match and pull out strings, $matches is the matches found.\r\n\r\n$matched = preg_match_all(\"\/<a\\s.[^<]*<\\\/a>\/\",$matchAStr,$matches);\r\necho \"There was $matched matche(s)\\n\";\r\n\r\necho \"<ol>\\n\";\r\nforeach($matches[0] as $url)\r\n{\r\n  echo \"<li>\".$url.\"<\/li>\\n\";\r\n}\r\necho \"<\/ol>\\n\";\r\n?>\r\n<\/pre>\n<pre lang=\"html\">\r\nThere was 2 matche(s)\r\n<ol>\r\n<li><a href=\"http:\/\/www.codingfriends.com\">codingfriends.com<\/a><\/li>\r\n<li><a href=\"http:\/\/www.norfolkhospice.org.uk\/\">norfolkhospice.org.uk<\/a><\/li>\r\n<\/ol>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Regular expression is when you have a match condition and a string to search through. Here is a cheat sheet to download to view regular expressions syntax. The regular matching method in php that I use is preg_match_all int preg_match_all ( string pattern, string subject, array &#038;matches [, int flags [, int offset]] ) which &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/22\/regular-expression-php\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Regular expression &#8211; PHP<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[64],"class_list":["post-561","post","type-post","status-publish","format-standard","hentry","category-php","tag-regular-expression"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=561"}],"version-history":[{"count":5,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/561\/revisions"}],"predecessor-version":[{"id":565,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/561\/revisions\/565"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}