{"id":739,"date":"2012-06-05T08:32:43","date_gmt":"2012-06-05T08:32:43","guid":{"rendered":"http:\/\/www.withinweb.com\/info\/?p=739"},"modified":"2012-06-20T13:26:32","modified_gmt":"2012-06-20T13:26:32","slug":"regular-expressions-part-4-array-processing","status":"publish","type":"post","link":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/","title":{"rendered":"Regular Expressions Part 4 &#8211; Array Processing"},"content":{"rendered":"<p>PHP&#8217;s preg_split() function enables you to break a string apart basing on something more complicated than a literal sequence of characters. When it&#8217;s necessary to split a string with a dynamic expression rather than a fixed one, this function comes to the rescue. The basic idea is the same as preg_match_all() except that, instead of returning matched pieces of the subject string, it returns an array of pieces that didn&#8217;t match the specified pattern. The following example uses a regular expression to split the string by any number of commas or space characters:<\/p>\n<p>&lt;?php<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n$keywords = preg_split(&#8220;\/[\\s,]+\/&#8221;, &#8220;php, regular expressions&#8221;);<br \/>\nprint_r( $keywords );<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n?&gt;<\/p>\n<p>Another useful PHP function is the preg_grep() function which returns those elements of an array that match a given pattern. This function traverses the input array, testing all elements against the supplied pattern. If a match is found, the matching element is returned as part of the array containing all matches. The following example searches through an array and all the names starting with letters A-J:<\/p>\n<p>&lt;?php<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n$names = array(&#8216;Andrew&#8217;,&#8217;John&#8217;,&#8217;Peter&#8217;,&#8217;Nastin&#8217;,&#8217;Bill&#8217;);<br \/>\n$output = preg_grep(&#8216;\/^[a-m]\/i&#8217;, $names);<br \/>\nprint_r( $output );<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n?&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP&#8217;s preg_split() function enables you to break a string apart basing on something more complicated than a literal sequence of characters. When it&#8217;s necessary to split a string with a dynamic expression rather than a fixed one, this function comes<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/\">Read more <span class=\"screen-reader-text\">Regular Expressions Part 4 &#8211; Array Processing<\/span><span class=\"meta-nav\"> &#8250;<\/span><\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,16],"tags":[],"class_list":["post-739","post","type-post","status-publish","format-standard","hentry","category-general-php","category-reg-expressions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Regular Expressions Part 4 - Array Processing - PHP Web Applications<\/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.withinweb.com\/info\/regular-expressions-part-4-array-processing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Regular Expressions Part 4 - Array Processing - PHP Web Applications\" \/>\n<meta property=\"og:description\" content=\"PHP&#8217;s preg_split() function enables you to break a string apart basing on something more complicated than a literal sequence of characters. When it&#8217;s necessary to split a string with a dynamic expression rather than a fixed one, this function comes&hellip;Read more Regular Expressions Part 4 &#8211; Array Processing &#8250;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Web Applications\" \/>\n<meta property=\"article:published_time\" content=\"2012-06-05T08:32:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-06-20T13:26:32+00:00\" \/>\n<meta name=\"author\" content=\"paulv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"paulv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-4-array-processing\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-4-array-processing\\\/\"},\"author\":{\"name\":\"paulv\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"headline\":\"Regular Expressions Part 4 &#8211; Array Processing\",\"datePublished\":\"2012-06-05T08:32:43+00:00\",\"dateModified\":\"2012-06-20T13:26:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-4-array-processing\\\/\"},\"wordCount\":208,\"commentCount\":0,\"articleSection\":[\"General PHP\",\"Regular Expressions\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-4-array-processing\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-4-array-processing\\\/\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-4-array-processing\\\/\",\"name\":\"Regular Expressions Part 4 - Array Processing - PHP Web Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\"},\"datePublished\":\"2012-06-05T08:32:43+00:00\",\"dateModified\":\"2012-06-20T13:26:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-4-array-processing\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-4-array-processing\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-4-array-processing\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Regular Expressions Part 4 &#8211; Array Processing\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\",\"name\":\"PHP Web Applications\",\"description\":\"Information and support for products of WithinWeb.com\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\",\"name\":\"paulv\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/author\\\/paulv\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Regular Expressions Part 4 - Array Processing - PHP Web Applications","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.withinweb.com\/info\/regular-expressions-part-4-array-processing\/","og_locale":"en_US","og_type":"article","og_title":"Regular Expressions Part 4 - Array Processing - PHP Web Applications","og_description":"PHP&#8217;s preg_split() function enables you to break a string apart basing on something more complicated than a literal sequence of characters. When it&#8217;s necessary to split a string with a dynamic expression rather than a fixed one, this function comes&hellip;Read more Regular Expressions Part 4 &#8211; Array Processing &#8250;","og_url":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/","og_site_name":"PHP Web Applications","article_published_time":"2012-06-05T08:32:43+00:00","article_modified_time":"2012-06-20T13:26:32+00:00","author":"paulv","twitter_card":"summary_large_image","twitter_misc":{"Written by":"paulv","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/#article","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/"},"author":{"name":"paulv","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"headline":"Regular Expressions Part 4 &#8211; Array Processing","datePublished":"2012-06-05T08:32:43+00:00","dateModified":"2012-06-20T13:26:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/"},"wordCount":208,"commentCount":0,"articleSection":["General PHP","Regular Expressions"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/","url":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/","name":"Regular Expressions Part 4 - Array Processing - PHP Web Applications","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/#website"},"datePublished":"2012-06-05T08:32:43+00:00","dateModified":"2012-06-20T13:26:32+00:00","author":{"@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"breadcrumb":{"@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-4-array-processing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.withinweb.com\/info\/"},{"@type":"ListItem","position":2,"name":"Regular Expressions Part 4 &#8211; Array Processing"}]},{"@type":"WebSite","@id":"https:\/\/www.withinweb.com\/info\/#website","url":"https:\/\/www.withinweb.com\/info\/","name":"PHP Web Applications","description":"Information and support for products of WithinWeb.com","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.withinweb.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c","name":"paulv","url":"https:\/\/www.withinweb.com\/info\/author\/paulv\/"}]}},"_links":{"self":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/comments?post=739"}],"version-history":[{"count":3,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/739\/revisions"}],"predecessor-version":[{"id":779,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/739\/revisions\/779"}],"wp:attachment":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/media?parent=739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/categories?post=739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/tags?post=739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}