{"id":813,"date":"2017-11-09T05:41:11","date_gmt":"2017-11-09T10:41:11","guid":{"rendered":"https:\/\/www.maxprog.com\/blog\/?p=813"},"modified":"2019-01-14T23:57:01","modified_gmt":"2019-01-15T04:57:01","slug":"xojo-nthfield-and-countfields-functions-in-php","status":"publish","type":"post","link":"https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/","title":{"rendered":"Xojo NthField() and CountFields() functions in PHP"},"content":{"rendered":"<p>In Xojo there is a handy function I use quite a lot for getting substrings between given delimiters from a piece of text, it is:<br \/>\n<code><br \/>\nNthField(text, delimiter, fieldnumber)<br \/>\n<\/code><br \/>\nImagine you have the following text &#8220;1\/2\/3\/4&#8221;, NthField( &#8220;1\/2\/3\/4&#8221;, &#8220;\/&#8221;, 3 ) will return 3, that is the third substring between the &#8220;\/&#8221; delimiters. It is that simple! This function works great in Xojo as long as the text is not too big. A few years ago I was porting a Xojo function to PHP that used NthField() so I decided to port the whole function. This is the PHP equivalent:<br \/>\n<code><br \/>\nfunction nthfield($text, $delimiter, $fieldnumber) {<br \/>\n\u00a0\u00a0$data = explode($delimiter, $text . $delimiter );<br \/>\n\u00a0\u00a0return $data[$fieldnumber-1];<br \/>\n}<br \/>\n<\/code><br \/>\nIt is that simple, now you have the exact same NthField() in PHP. That function is fully accurate, I have been using it everywhere in my site for years.<\/p>\n<p>Next I needed the related function to count the number of fields for a given delimiter:<br \/>\n<code><br \/>\nCountFields(text, delimiter)<br \/>\n<\/code><br \/>\nFor the sample text we used above, &#8220;1\/2\/3\/4&#8221;, CountFields( &#8220;1\/2\/3\/4&#8221;, &#8220;\/&#8221; ) would return 4, that is, the number of fields enclosed by the &#8220;\/&#8221; delimiter. This is the PHP equivalent:<br \/>\n<code><br \/>\nfunction countfields($text, $delimiter) {<br \/>\n\u00a0\u00a0return count(explode($delimiter, $text));<br \/>\n}<br \/>\n<\/code><br \/>\nAs you can see the both functions use simple PHP array functions and since NthField() is slow in Xojo when the text is big, it would be simple to rewrite it \ud83d\ude42<\/p>\n<p>\u2014<br \/>\nStan Busk &#8211; Software Engineer<br \/>\nat <a href=\"https:\/\/www.maxprog.com\" target=\"_blank\">www.maxprog.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Xojo there is a handy function I use quite a lot for getting substrings between given delimiters from a piece of text, it is: NthField(text, delimiter, fieldnumber) Imagine you have the following text &#8220;1\/2\/3\/4&#8221;, NthField( &#8220;1\/2\/3\/4&#8221;, &#8220;\/&#8221;, 3 ) will return 3, that is the third substring between the &#8220;\/&#8221; delimiters. It is that simple! This function works great in Xojo as long as the text is not too big. A few years ago I was porting a Xojo function to PHP that used NthField() so I decided to port the whole function. This is the PHP equivalent: function &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[55,60,56],"class_list":["post-813","post","type-post","status-publish","format-standard","hentry","category-php","tag-php","tag-text-handling","tag-xojo"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Xojo NthField() and CountFields() functions in PHP - Tips and tricks<\/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.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Xojo NthField() and CountFields() functions in PHP\" \/>\n<meta property=\"og:description\" content=\"In Xojo there is a handy function I use quite a lot for getting substrings between given delimiters from a piece of text, it is: NthField(text, delimiter, fieldnumber) Imagine you have the following text &#8220;1\/2\/3\/4&#8221;, NthField( &#8220;1\/2\/3\/4&#8221;, &#8220;\/&#8221;, 3 ) will return 3, that is the third substring between the &#8220;\/&#8221; delimiters. It is that simple! This function works great in Xojo as long as the text is not too big. A few years ago I was porting a Xojo function to PHP that used NthField() so I decided to port the whole function. This is the PHP equivalent: function &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Tips and tricks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/maxprog\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/maxprog\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-09T10:41:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-01-15T04:57:01+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@maxprog\" \/>\n<meta name=\"twitter:site\" content=\"@maxprog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\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.maxprog.com\\\/blog\\\/xojo-nthfield-and-countfields-functions-in-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/xojo-nthfield-and-countfields-functions-in-php\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/#\\\/schema\\\/person\\\/94903012a832903f334e8b939c2ac916\"},\"headline\":\"Xojo NthField() and CountFields() functions in PHP\",\"datePublished\":\"2017-11-09T10:41:11+00:00\",\"dateModified\":\"2019-01-15T04:57:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/xojo-nthfield-and-countfields-functions-in-php\\\/\"},\"wordCount\":207,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/#organization\"},\"keywords\":[\"PHP\",\"Text handling\",\"Xojo\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/xojo-nthfield-and-countfields-functions-in-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/xojo-nthfield-and-countfields-functions-in-php\\\/\",\"url\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/xojo-nthfield-and-countfields-functions-in-php\\\/\",\"name\":\"Xojo NthField() and CountFields() functions in PHP - Tips and tricks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/#website\"},\"datePublished\":\"2017-11-09T10:41:11+00:00\",\"dateModified\":\"2019-01-15T04:57:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/xojo-nthfield-and-countfields-functions-in-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/xojo-nthfield-and-countfields-functions-in-php\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/xojo-nthfield-and-countfields-functions-in-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Xojo NthField() and CountFields() functions in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/\",\"name\":\"Tips and tricks\",\"description\":\"How to use Maxprog products\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/#organization\",\"name\":\"Maxprog\",\"url\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/04\\\/logo_maxprog_pict_white_with_blue_back_512x512.png\",\"contentUrl\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/04\\\/logo_maxprog_pict_white_with_blue_back_512x512.png\",\"width\":512,\"height\":512,\"caption\":\"Maxprog\"},\"image\":{\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/maxprog\",\"https:\\\/\\\/x.com\\\/maxprog\",\"https:\\\/\\\/www.instagram.com\\\/stanbusk\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/maxprog\",\"https:\\\/\\\/es.pinterest.com\\\/stanbusk\\\/\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/Maxprog\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.maxprog.com\\\/blog\\\/#\\\/schema\\\/person\\\/94903012a832903f334e8b939c2ac916\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/49daac01857e4ec6646801107e47c7f62856c94d4ef7caf1a296f296c790fb81?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/49daac01857e4ec6646801107e47c7f62856c94d4ef7caf1a296f296c790fb81?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/49daac01857e4ec6646801107e47c7f62856c94d4ef7caf1a296f296c790fb81?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"description\":\"Software engineer at maxprog.com\",\"sameAs\":[\"https:\\\/\\\/www.maxprog.com\\\/\",\"https:\\\/\\\/www.facebook.com\\\/maxprog\",\"https:\\\/\\\/www.instagram.com\\\/maxprog8\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/maxprog\",\"https:\\\/\\\/www.pinterest.es\\\/stanbusk\",\"https:\\\/\\\/x.com\\\/maxprog\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/Maxprog\",\"https:\\\/\\\/maxprog.tumblr.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Xojo NthField() and CountFields() functions in PHP - Tips and tricks","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.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Xojo NthField() and CountFields() functions in PHP","og_description":"In Xojo there is a handy function I use quite a lot for getting substrings between given delimiters from a piece of text, it is: NthField(text, delimiter, fieldnumber) Imagine you have the following text &#8220;1\/2\/3\/4&#8221;, NthField( &#8220;1\/2\/3\/4&#8221;, &#8220;\/&#8221;, 3 ) will return 3, that is the third substring between the &#8220;\/&#8221; delimiters. It is that simple! This function works great in Xojo as long as the text is not too big. A few years ago I was porting a Xojo function to PHP that used NthField() so I decided to port the whole function. This is the PHP equivalent: function &hellip;","og_url":"https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/","og_site_name":"Tips and tricks","article_publisher":"https:\/\/www.facebook.com\/maxprog","article_author":"https:\/\/www.facebook.com\/maxprog","article_published_time":"2017-11-09T10:41:11+00:00","article_modified_time":"2019-01-15T04:57:01+00:00","author":"admin","twitter_card":"summary_large_image","twitter_creator":"@maxprog","twitter_site":"@maxprog","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/#article","isPartOf":{"@id":"https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/"},"author":{"name":"admin","@id":"https:\/\/www.maxprog.com\/blog\/#\/schema\/person\/94903012a832903f334e8b939c2ac916"},"headline":"Xojo NthField() and CountFields() functions in PHP","datePublished":"2017-11-09T10:41:11+00:00","dateModified":"2019-01-15T04:57:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/"},"wordCount":207,"commentCount":0,"publisher":{"@id":"https:\/\/www.maxprog.com\/blog\/#organization"},"keywords":["PHP","Text handling","Xojo"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/","url":"https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/","name":"Xojo NthField() and CountFields() functions in PHP - Tips and tricks","isPartOf":{"@id":"https:\/\/www.maxprog.com\/blog\/#website"},"datePublished":"2017-11-09T10:41:11+00:00","dateModified":"2019-01-15T04:57:01+00:00","breadcrumb":{"@id":"https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.maxprog.com\/blog\/xojo-nthfield-and-countfields-functions-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.maxprog.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Xojo NthField() and CountFields() functions in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.maxprog.com\/blog\/#website","url":"https:\/\/www.maxprog.com\/blog\/","name":"Tips and tricks","description":"How to use Maxprog products","publisher":{"@id":"https:\/\/www.maxprog.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.maxprog.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.maxprog.com\/blog\/#organization","name":"Maxprog","url":"https:\/\/www.maxprog.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.maxprog.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.maxprog.com\/blog\/wp-content\/uploads\/2019\/04\/logo_maxprog_pict_white_with_blue_back_512x512.png","contentUrl":"https:\/\/www.maxprog.com\/blog\/wp-content\/uploads\/2019\/04\/logo_maxprog_pict_white_with_blue_back_512x512.png","width":512,"height":512,"caption":"Maxprog"},"image":{"@id":"https:\/\/www.maxprog.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/maxprog","https:\/\/x.com\/maxprog","https:\/\/www.instagram.com\/stanbusk\/","https:\/\/www.linkedin.com\/company\/maxprog","https:\/\/es.pinterest.com\/stanbusk\/","https:\/\/www.youtube.com\/c\/Maxprog"]},{"@type":"Person","@id":"https:\/\/www.maxprog.com\/blog\/#\/schema\/person\/94903012a832903f334e8b939c2ac916","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/49daac01857e4ec6646801107e47c7f62856c94d4ef7caf1a296f296c790fb81?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/49daac01857e4ec6646801107e47c7f62856c94d4ef7caf1a296f296c790fb81?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/49daac01857e4ec6646801107e47c7f62856c94d4ef7caf1a296f296c790fb81?s=96&d=mm&r=g","caption":"admin"},"description":"Software engineer at maxprog.com","sameAs":["https:\/\/www.maxprog.com\/","https:\/\/www.facebook.com\/maxprog","https:\/\/www.instagram.com\/maxprog8\/","https:\/\/www.linkedin.com\/company\/maxprog","https:\/\/www.pinterest.es\/stanbusk","https:\/\/x.com\/maxprog","https:\/\/www.youtube.com\/c\/Maxprog","https:\/\/maxprog.tumblr.com"]}]}},"_links":{"self":[{"href":"https:\/\/www.maxprog.com\/blog\/wp-json\/wp\/v2\/posts\/813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.maxprog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.maxprog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.maxprog.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.maxprog.com\/blog\/wp-json\/wp\/v2\/comments?post=813"}],"version-history":[{"count":0,"href":"https:\/\/www.maxprog.com\/blog\/wp-json\/wp\/v2\/posts\/813\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.maxprog.com\/blog\/wp-json\/wp\/v2\/media?parent=813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.maxprog.com\/blog\/wp-json\/wp\/v2\/categories?post=813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.maxprog.com\/blog\/wp-json\/wp\/v2\/tags?post=813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}