{"id":906,"date":"2019-10-18T02:04:06","date_gmt":"2019-10-18T02:04:06","guid":{"rendered":"http:\/\/www.projectimmerse.com\/blog\/?p=906"},"modified":"2020-06-15T09:56:41","modified_gmt":"2020-06-15T09:56:41","slug":"substr-vs-preg_match-using-php","status":"publish","type":"post","link":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/","title":{"rendered":"substr() vs preg_match() using PHP"},"content":{"rendered":"\r\n<p>Greetings fellow developers, I hope you&#8217;ve continued to immerse yourself in something interesting whether it&#8217;s programming, physics, math &#8211; or whatever. The point is &#8211; just keep pushing forward and don&#8217;t be afraid to fail. Seriously though, enjoy the journey of making mistakes.<\/p>\r\n\r\n<p>This post is about two functions &#8211; PHP&#8217;s <a href=\"https:\/\/www.php.net\/manual\/en\/function.substr.php\" title=\"substr()\">substr()<\/a> and <a href=\"https:\/\/www.php.net\/manual\/en\/function.preg-match.php\" title=\"preg_match()\">preg_match()<\/a> functions.<\/p>\r\n\r\n<!--more-->\r\n\r\n<p>The reason why I want to emphasize on these two specific functions is because recently in my consulting work I run into some deep code that required my replacing a subtr() function with a preg_match() function. Now, mind you &#8211; finding this required some deep digging, hence the phrase &#8220;deep code&#8221;.<\/p>\r\n\r\nThe snippet of code I was working with looked like the code below:\r\n\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n$string = get_field('popup_slider',false,false);\r\n$arr = substr($string, -6,4);\r\n\r\n\r\n\/\/ Where $string would be pulling some kind of string like this example - &#x5B;soliloquy id=&quot;9008&quot;]\r\n\r\n\/\/ yeah I was working with a soliloquy plugin (hook)\r\n\r\n<\/pre>\r\n\r\n<p>The issue with this though as I learned is it&#8217;s reliability would break if we have an &#8220;id&#8221; longer than 4 digits or characters. Which was exactly what was happening in this case.<\/p>\r\n\r\n<p>And so the fix this with a more reliable method, I decided to use preg_match() and some regex to capture what I needed. Check out the code below:<\/p>\r\n\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\npreg_match('\/id=&quot;(&#x5B;0-9]+)&quot;\/', $string, $matches);\r\n\/\/print_r($matches);\r\n$arr = $matches&#x5B;1];\r\n\r\n<\/pre>\r\n\r\n<p>So with using preg_match() we can capture any digits of any length and ensure that they aren&#8217;t cut off. We pass the $string function and the third argument would be the result once the first argument (regex) is applied.<\/p>\r\n\r\n<p>I would say that actually finding the issue itself was the more difficult task. And then finding a stable solution required some research. For me preg_match() seems quite stable &#8211; I&#8217;m curious to find out if there is another optimal solution.<\/p>\r\n\r\n<p>Stay tuned for more nerdy posts &#8211; until then, keep immersing yourself.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Greetings fellow developers, I hope you&#8217;ve continued to immerse yourself in something interesting whether it&#8217;s programming, physics, math &#8211; or whatever. The point is &#8211; just keep pushing forward and don&#8217;t be afraid to fail. Seriously though, enjoy the journey of making mistakes. This post is about two functions &#8211; PHP&#8217;s substr() and preg_match() functions.<\/p>\n","protected":false},"author":1,"featured_media":1094,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-906","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>substr() vs preg_match() using PHP - Project Immerse<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"substr() vs preg_match() using PHP - Project Immerse\" \/>\n<meta property=\"og:description\" content=\"Greetings fellow developers, I hope you&#8217;ve continued to immerse yourself in something interesting whether it&#8217;s programming, physics, math &#8211; or whatever. The point is &#8211; just keep pushing forward and don&#8217;t be afraid to fail. Seriously though, enjoy the journey of making mistakes. This post is about two functions &#8211; PHP&#8217;s substr() and preg_match() functions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Project Immerse\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-18T02:04:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-15T09:56:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/10\/substr-vs-preg-match-using-php.png\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"750\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"projectimmerse\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"projectimmerse\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/\"},\"author\":{\"name\":\"projectimmerse\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"headline\":\"substr() vs preg_match() using PHP\",\"datePublished\":\"2019-10-18T02:04:06+00:00\",\"dateModified\":\"2020-06-15T09:56:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/\"},\"wordCount\":343,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/substr-vs-preg-match-using-php.png\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/\",\"name\":\"substr() vs preg_match() using PHP - Project Immerse\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/substr-vs-preg-match-using-php.png\",\"datePublished\":\"2019-10-18T02:04:06+00:00\",\"dateModified\":\"2020-06-15T09:56:41+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/substr-vs-preg-match-using-php.png\",\"contentUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/substr-vs-preg-match-using-php.png\",\"width\":750,\"height\":750,\"caption\":\"substr vs preg_match\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/substr-vs-preg_match-using-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"substr() vs preg_match() using PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\",\"name\":\"Project Immerse\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\",\"name\":\"projectimmerse\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"caption\":\"projectimmerse\"},\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/author\\\/projectimmerse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"substr() vs preg_match() using PHP - Project Immerse","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/","og_locale":"en_US","og_type":"article","og_title":"substr() vs preg_match() using PHP - Project Immerse","og_description":"Greetings fellow developers, I hope you&#8217;ve continued to immerse yourself in something interesting whether it&#8217;s programming, physics, math &#8211; or whatever. The point is &#8211; just keep pushing forward and don&#8217;t be afraid to fail. Seriously though, enjoy the journey of making mistakes. This post is about two functions &#8211; PHP&#8217;s substr() and preg_match() functions.","og_url":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/","og_site_name":"Project Immerse","article_published_time":"2019-10-18T02:04:06+00:00","article_modified_time":"2020-06-15T09:56:41+00:00","og_image":[{"width":750,"height":750,"url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/10\/substr-vs-preg-match-using-php.png","type":"image\/png"}],"author":"projectimmerse","twitter_card":"summary_large_image","twitter_misc":{"Written by":"projectimmerse","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/#article","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/"},"author":{"name":"projectimmerse","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"headline":"substr() vs preg_match() using PHP","datePublished":"2019-10-18T02:04:06+00:00","dateModified":"2020-06-15T09:56:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/"},"wordCount":343,"commentCount":0,"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/10\/substr-vs-preg-match-using-php.png","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/","url":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/","name":"substr() vs preg_match() using PHP - Project Immerse","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/#primaryimage"},"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/10\/substr-vs-preg-match-using-php.png","datePublished":"2019-10-18T02:04:06+00:00","dateModified":"2020-06-15T09:56:41+00:00","author":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"breadcrumb":{"@id":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/#primaryimage","url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/10\/substr-vs-preg-match-using-php.png","contentUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2019\/10\/substr-vs-preg-match-using-php.png","width":750,"height":750,"caption":"substr vs preg_match"},{"@type":"BreadcrumbList","@id":"https:\/\/www.projectimmerse.com\/blog\/substr-vs-preg_match-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.projectimmerse.com\/blog\/"},{"@type":"ListItem","position":2,"name":"substr() vs preg_match() using PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.projectimmerse.com\/blog\/#website","url":"https:\/\/www.projectimmerse.com\/blog\/","name":"Project Immerse","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.projectimmerse.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5","name":"projectimmerse","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","caption":"projectimmerse"},"url":"https:\/\/www.projectimmerse.com\/blog\/author\/projectimmerse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/comments?post=906"}],"version-history":[{"count":4,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/906\/revisions"}],"predecessor-version":[{"id":912,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/906\/revisions\/912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media\/1094"}],"wp:attachment":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media?parent=906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/categories?post=906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/tags?post=906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}