1

Say I have the following array of strings:

$data_array = array("the dog is hairy", "cats like treats", "my mouse is tired");

I want to write a function that will extract an element from the array based on whether it contains another string.

For example, if the array element contains the string "dog" i want to return the string "the dog is hairy" for use elsewhere.

I tried using a foreach loop but it didn't work:

foreach ($data_array as $sentence){
    if (stripos($sentence, "dog")){
        echo $sentence;
    }
}

What is the best way to go about this?

3
  • It looks like it's working? codepad.org/BpYE0lHf Commented Oct 9, 2011 at 0:39
  • 3
    And as @AurelioDeRosa points out, you're best checking for a non-false value since 0 could be interpreted as a falsy value. Commented Oct 9, 2011 at 0:42
  • yep that's what was happening, thanks Commented Oct 9, 2011 at 0:50

5 Answers 5

2

If you want to use stripos this is the code:

   $data_array = array("the dog is hairy", "cats like treats", "my mouse is tired");
   foreach($data_array as $value)
   {
      if (stripos($value, 'dog') !== FALSE)
         echo $value;
   }
Sign up to request clarification or add additional context in comments.

1 Comment

no need to use foreach or any loop process to do it. Check my example.
2

It works fine for me.

http://sandbox.phpcode.eu/g/064bf

<?php 
$data_array = array("the dog is hairy", "cats like treats", "my mouse is tired"); 
foreach($data_array as $data){ 
    if (false !== stripos($data, "dog")){ 
        echo $data; 
    } 
}

1 Comment

The third sentence (about using an appropriate variable for stripos()). The OP doesn't seem to suggest this was a problem.
1
foreach ($data_array as $val) {
  if (strstr($val, 'dog')) {
    echo $val;
  }
}

1 Comment

Sorry I just edited my question. I tried that and it didn't work for some reason.
0

You can use this example:

$f_array = preg_grep('/dog/i', $data_array);  

Comments

-1

You could also use preg_grep():

$found_array = preg_grep( '/dog/i', $data_array );

Edit: Added /i switch for case-insensitive matching.

6 Comments

This solution slow the performance. RegEx are always slower than string comparison.
Regex compilation does carry an overhead, but so does foreach. I haven't done a benchmark, but I wouldn't be surprised if this wasn't faster than the foreach-based solutions.
@AurelioDeRosa: I did run a quick benchmark, and it seems that using preg_grep() is from twice (for the three-element input array given by the OP) to four times (for a 98000 word dictionary) faster than using foreach and stripos() as in your answer.
You answered yourself. Test regex in a real contest. Use an array of thousand element where every value have a long lenght and then the test will says something correct. Anyway you can ask to everyone you want and everybody will tell you that the regex have to be used mildly.
@AurelioDeRosa: OK, since you insist: 1000 lines (of which 220 match), 302262 bytes total (avg. 302.262 bytes/line), repeated 10000 times: preg_grep() 15.940 seconds; stripos() 31.509 seconds. If you don't find these benchmarks convincing, why not try running your own?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.