{"id":851,"date":"2020-04-19T16:03:39","date_gmt":"2020-04-19T15:03:39","guid":{"rendered":"https:\/\/blog.pgregg.com\/blog\/?p=851"},"modified":"2020-04-19T16:04:19","modified_gmt":"2020-04-19T15:04:19","slug":"php-strnpos-function","status":"publish","type":"post","link":"https:\/\/blog.pgregg.com\/blog\/2020\/04\/php-strnpos-function\/","title":{"rendered":"PHP strnpos function"},"content":{"rendered":"\n<p><em><span class=\"has-inline-color has-vivid-red-color\">Note: I&#8217;m moving a bunch of old web pages into my blog. This code is from 2003, it may have some use to someone still.<\/span><\/em><\/p>\n\n\n\n<p>PHP provides a few similar functions, but not this specific one.<\/p>\n\n\n\n<p>If you want to find the first occurrence of a substring in a string you have <a href=\"https:\/\/www.php.net\/manual\/en\/function.strpos.php\">strpos()<\/a><\/p>\n\n\n\n<p>If you want to find the last occurrence of a substring in a string you have <a href=\"https:\/\/www.php.net\/manual\/en\/function.strrpos.php\">strrpos()<\/a><\/p>\n\n\n\n<p>But what if you want to find the n<sup>th<\/sup> occurrence? Enter strnpos():<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/*\n * Find the nth occurance of a string in another string\n *\n * Paul Gregg &lt;pgregg@pgregg.com>\n * 23 September 2003\n *\n * Open Source Code:   If you use this code on your site for public\n * access (i.e. on the Internet) then you must attribute the author and\n * source web site: http:\/\/www.pgregg.com\/projects\/php\/code\/strnpos.phps\n *\n *\/\n\n\n\/\/ Optimal solution\nFunction strnpos($haystack, $needle, $nth=1, $offset=0) {\n  if ($nth &lt; 1) $nth = 1;\n  $loop1 = TRUE;\n  while ($nth > 0) {\n    $offset = strpos($haystack, $needle, $loop1 ? $offset : $offset+1);\n    if ($offset === FALSE) break;\n    $nth--;\n    $loop1 = FALSE;\n  }\n  return $offset;\n}\n\n\/\/ Interesting solution without using strpos (without offset capability)\nFunction strnpos2($haystack, $needle, $nth=1) {\n  if ($nth &lt; 1) $nth = 1;\n  $arr = explode($needle, $haystack);\n  if ($nth > (count($arr)-1)) return FALSE;\n  $str = implode($needle, array_slice($arr, 0, $nth));\n  return strlen($str);\n}\n?><\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/pgregg.com\/projects\/php\/code\/strnpos.phps\">Source code can be found here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: I&#8217;m moving a bunch of old web pages into my blog. This code is from 2003, it may have some use to someone still. PHP provides a few similar functions, but not this specific one. If you want to find the first occurrence of a substring in a string you have strpos() If you &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.pgregg.com\/blog\/2020\/04\/php-strnpos-function\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PHP strnpos function&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-851","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbQOUu-dJ","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/blog.pgregg.com\/blog\/wp-json\/wp\/v2\/posts\/851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.pgregg.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.pgregg.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.pgregg.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.pgregg.com\/blog\/wp-json\/wp\/v2\/comments?post=851"}],"version-history":[{"count":2,"href":"https:\/\/blog.pgregg.com\/blog\/wp-json\/wp\/v2\/posts\/851\/revisions"}],"predecessor-version":[{"id":853,"href":"https:\/\/blog.pgregg.com\/blog\/wp-json\/wp\/v2\/posts\/851\/revisions\/853"}],"wp:attachment":[{"href":"https:\/\/blog.pgregg.com\/blog\/wp-json\/wp\/v2\/media?parent=851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.pgregg.com\/blog\/wp-json\/wp\/v2\/categories?post=851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.pgregg.com\/blog\/wp-json\/wp\/v2\/tags?post=851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}