Jump to content


Photo

Best way to search result with ARRAY of needles?


Best Answer AbraCadaver, 12 December 2013 - 11:53 AM

If all you need is the count, here's one way:

str_replace($needle, null, $result, $count);
echo $count;

 

Go to the full post


  • Please log in to reply
4 replies to this topic

#1 RuleBritannia

RuleBritannia

    Advanced Member

  • Members
  • PipPipPip
  • 93 posts

Posted 12 December 2013 - 11:11 AM

Hello

I 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.



#2 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,145 posts

Posted 12 December 2013 - 11:27 AM

 

 

What do you think is the best way to achieve this?

I'm thinking foreach loop

You just answered your question.  ;D



#3 RuleBritannia

RuleBritannia

    Advanced Member

  • Members
  • PipPipPip
  • 93 posts

Posted 12 December 2013 - 11:35 AM

You just answered your question.  ;D

Hello

 

Yes I am aware a foreach can do it, but just wondering if there are any other alternatives(built in functions etc)



#4 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,888 posts
  • LocationThe Republic of Texas

Posted 12 December 2013 - 11:53 AM   Best Answer

If all you need is the count, here's one way:

str_replace($needle, null, $result, $count);
echo $count;

 


mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#5 RuleBritannia

RuleBritannia

    Advanced Member

  • Members
  • PipPipPip
  • 93 posts

Posted 12 December 2013 - 12:46 PM

 

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!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com