RuleBritannia Posted December 12, 2013 Share Posted December 12, 2013 HelloI want to get a count of how many needles are found.Im aware there are some different ways to search a result with 1 needle, But If I have an array of needles$result = 'fgnfmgn NEED this 22 STRING dgfdfsgfdgfd NEEDS 55';$needle[0] = 'NEED this 22 STRING';$needle[1] = 'NEEDS 55';Most functions like substr_count and preg_match_all, will work for 1 needle as a string, but wont accept more than one needle in the form of a array.What do you think is the best way to achieve this?I'm thinking foreach loop with substr_count, But wanted to ask here encase there is a simpler way of achieving this.Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/284733-best-way-to-search-result-with-array-of-needles/ Share on other sites More sharing options...
Ch0cu3r Posted December 12, 2013 Share Posted December 12, 2013 What do you think is the best way to achieve this?I'm thinking foreach loop You just answered your question. Quote Link to comment https://forums.phpfreaks.com/topic/284733-best-way-to-search-result-with-array-of-needles/#findComment-1462201 Share on other sites More sharing options...
RuleBritannia Posted December 12, 2013 Author Share Posted December 12, 2013 You just answered your question. Hello Yes I am aware a foreach can do it, but just wondering if there are any other alternatives(built in functions etc) Quote Link to comment https://forums.phpfreaks.com/topic/284733-best-way-to-search-result-with-array-of-needles/#findComment-1462202 Share on other sites More sharing options...
Solution AbraCadaver Posted December 12, 2013 Solution Share Posted December 12, 2013 If all you need is the count, here's one way: str_replace($needle, null, $result, $count); echo $count; Quote Link to comment https://forums.phpfreaks.com/topic/284733-best-way-to-search-result-with-array-of-needles/#findComment-1462203 Share on other sites More sharing options...
RuleBritannia Posted December 12, 2013 Author Share Posted December 12, 2013 If all you need is the count, here's one way: str_replace($needle, null, $result, $count); echo $count; Excellent answer, This is exactly why I post questions like these. Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/284733-best-way-to-search-result-with-array-of-needles/#findComment-1462211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.