{"flag":true,"single":true,"pageTitle":"Regular expressions in php","post":{"id":181,"user_id":"1","slug":"regular-expressions-in-php-yvrd","title":"Regular expressions in php","body":"<p>Functions:<\/p>\r\n<p><strong>syntax: <\/strong>function_name('\/pattern\/',string);<\/p>\r\n<p><strong>preg_match<\/strong> &ndash;find pattern match on a string. returns true if found else false<\/p>\r\n<p><strong>preg_split<\/strong> &ndash; find pattern match on a string and then split the results into a numeric array<\/p>\r\n<p><strong>preg_replace<\/strong> &ndash; find pattern match on a string and then replace the match with the specified text.<\/p>\r\n<p><strong>USE OF REGULAR EXPRESSIONS<\/strong><\/p>\r\n<ul>\r\n<li>identifying patterns in the string,<\/li>\r\n<li>validating user input,<\/li>\r\n<li>Highlighting keywords in search results,&nbsp;<\/li>\r\n<\/ul>\r\n<p><strong>preg_match<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\n$my_url = \"www.rizikmw.com\";\r\nif (preg_match(\"\/rizikmw\/\", $my_url))\r\n{\r\n\techo \"the url $my_url contains rizikmw\";\r\n}\r\nelse\r\n{\r\n\techo \"the url $my_url does not contain rizikmw\";\r\n}\r\n?&gt;<\/code><\/pre>\r\n<p><strong>preg_split<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\n$my_text=\"I Love R\";\r\n$my_array  = preg_split(\"\/ \/\", $my_text);\r\nprint_r($my_array );\r\n?&gt;<\/code><\/pre>\r\n<p><strong>preg_replace<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\n$text = \"We are Gondals and Gondals are amazing.\";\r\n$text = preg_replace(\"\/Gondals\/\", '&lt;span style=\"background:yellow\"&gt;Gondals&lt;\/span&gt;', $text);\r\necho $text;\r\n?&gt;<\/code><\/pre>\r\n<p><strong>SOME REGULAR EXPRESSIONS<\/strong><\/p>\r\n<ul>\r\n<li>echo \"&lt;b&gt;Hello 0ops 03468434644&lt;\/b&gt;&lt;br\/&gt;\";<\/li>\r\n<li>$str=\"Hello 0ops 03468434644\";<\/li>\r\n<li>echo preg_replace(\"\/[4o]\/\", \"X\", $str).\"&lt;br\/&gt;\";\/\/<strong>replace all 4 with X<\/strong><\/li>\r\n<li>echo preg_replace(\"\/.\/\", \"X\", $str).\"&lt;br\/&gt;\";\/\/<strong>replace every chraters by X, XXXXXXXXXXX<\/strong><\/li>\r\n<li>echo preg_replace(\"\/.....\/\", \"X\", $str).\"&lt;br\/&gt;\";\/\/<strong>replace every five chraters (left to right) by X and print remaining, XXXX44<\/strong><\/li>\r\n<li>echo preg_replace(\"\/^Hello\/\", \"X\", $str).\"&lt;br\/&gt;\"; \/\/ <strong>any string that starts with hello<\/strong><\/li>\r\n<li>echo preg_replace(\"\/44$\/\", \"X\", $str).\"&lt;br\/&gt;\"; \/\/\/ 44$\/ <strong>matches all strings that have 44 at end. check .com domains using this .com$<\/strong><\/li>\r\n<li>echo preg_replace(\"\/0346+8434644\/\", \"X\", $str).\"&lt;br\/&gt;\"; \/\/<strong>preceding value must be Gon<\/strong><\/li>\r\n<li>echo preg_replace(\"\/Gon\\+d\/\", \"X\", $str).\"&lt;br\/&gt;\"; \/\/<strong>treat + as string, use \\escape ,this is not for preceding<\/strong><\/li>\r\n<li>echo preg_replace(\"\/[a]\/\", \"X\", $str).\"&lt;br\/&gt;\"; \/\/<strong>replace all a with X<\/strong><\/li>\r\n<li>echo preg_replace(\"\/[a-z]\/\", \"X\", $str).\"&lt;br\/&gt;\"; \/\/<strong>replace all lower case charters with X<\/strong><\/li>\r\n<li>echo preg_replace(\"\/[A-Z]\/\", \"X\", $str).\"&lt;br\/&gt;\";\/\/<strong>replace all upper case charters with X<\/strong><\/li>\r\n<li>echo preg_replace(\"\/[0-9]\/\", \"X\", $str).\"&lt;br\/&gt;\";\/\/replace all 0,1,2,3,4,5,6,7,8,9 with X<\/li>\r\n<li>echo preg_replace(\"\/[0-3]\/\", \"X\", $str).\"&lt;br\/&gt;\";\/\/replace all 0,1,2,3 with X<\/li>\r\n<li>echo preg_replace(\"\/[4]\/\", \"X\", $str).\"&lt;br\/&gt;\";\/\/replace all 4 with X<\/li>\r\n<li>echo preg_replace(\"\/[4p]\/\", \"X\", $str).\"&lt;br\/&gt;\";\/\/replace all 4 or p with X<\/li>\r\n<li>\r\n<pre class=\"language-markup\"><code>$my_email = \"00@company.com\";\r\nif (preg_match(\"\/^[a-zA-Z0-9._-]{2,3}+@[a-zA-Z0-9-]+\\.[a-zA-Z.]{2,5}$\/\", $my_email)) {\r\n\/\/\"^[a-zA-Z0-9._-]\" matches any lower or upper case letters, numbers between 0 and 9 and dots, underscores or dashes.\r\n\/\/\"+@[a-zA-Z0-9-]\" matches the @ symbol followed by lower or upper case letters, numbers between 0 and 9 or dashes.\r\n\/\/\"+\\.[a-zA-Z.]{2,5}$\/\" escapes the dot using the backslash then matches any lower or upper case letters with a character length between 2 and 5 at the end of the string.\r\necho \"$my_email is a valid email address\";\r\n}\r\nelse\r\n{\r\necho \"$my_email is NOT a valid email address\";\r\n}\r\n\r\n<\/code><\/pre>\r\n<\/li>\r\n<\/ul>\r\n<p><strong>Extract specific data from string it will extract <span style=\"background-color: #2dc26b;\">fafa no 1<\/span><\/strong><\/p>\r\n<pre class=\"language-markup\"><code>$str=mynama is done and i am fafa no 1: ok\r\npreg_match('\/fa-(.*?):\/', $str, $res);\r\npritn_r($res);<\/code><\/pre>\r\n<p><strong>Split stric on basis on one or more spaces like tab<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>$parts = preg_split('\/\\s+\/', $value);<\/code><\/pre>\r\n<p><a href=\"https:\/\/cheatography.com\/davechild\/cheat-sheets\/regular-expressions\/\">Regular Expressions Cheat Sheet by DaveChild - Download free from Cheatography - Cheatography.com: Cheat Sheets For Every Occasion<\/a><\/p>","category_id":"1","is_private":"0","created_at":"2023-09-12T23:03:11.000000Z","updated_at":"2023-09-12T23:03:11.000000Z","category":{"id":1,"user_id":"1","name":"PHP","slug":"php-3ius","parent_id":null,"created_at":"2023-03-14T03:58:19.000000Z","updated_at":"2023-03-14T03:58:19.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"Functions: syntax: function_name('\/pattern\/',string); preg_match &ndash;find pattern match on a string. returns true if found else false pre - Regular expressions in php (Updated: September 12, 2023) - Read more about Regular expressions in php at my programming site [SITE]","categories":[]}