5

I have been looking around for a while in the PHP manual and can't find any command that does what I want.

I have an array with Keys and Values, example:

$Fields = array("Color"=>"Bl","Taste"=>"Good","Height"=>"Tall");

Then I have a string, for example:

$Headline = "My black coffee is cold";

Now I want to find out if any of the array ($Fields) values match somewhere in the string ($Headline).

Example:

Array_function_xxx($Headline,$Fields);

Would give the result true because "bl" is in the string $Headline (as a part of "Black").

I'm asking because I need performance... If this isn't possible, I will just make my own function instead...

EDIT - I'm looking for something like stristr(string $haystack , array $needle);

Thanks

SOLUTION - I came up with his function.

function array_in_str($fString, $fArray) {

  $rMatch = array();

  foreach($fArray as $Value) {
    $Pos = stripos($fString,$Value);
    if($Pos !== false)
      // Add whatever information you need
      $rMatch[] = array( "Start"=>$Pos,
                         "End"=>$Pos+strlen($Value)-1,
                         "Value"=>$Value
                       );
  }

  return $rMatch;
}

The returning array now have information on where each matched word begins and ends.

Max Kielland
  • 5,627
  • 9
  • 60
  • 95

2 Answers2

5

This should help:

function Array_function_xxx($headline, $fields) {
    $field_values = array_values($fields);
    foreach ($field_values as $field_value) {
        if (strpos($headline, $field_value) !== false) {
            return true; // field value found in a string
        }
    }
    return false; // nothing found during the loop
}

Replace name of the function with what you need.

EDIT:

Ok, alternative solution (probably giving better performance, allowing for case-insensitive search, but requiring proper values within $fields parameter) is:

function Array_function_xxx($headline, $fields) {
    $regexp = '/(' . implode('|',array_values($fields)) . ')/i';
    return (bool) preg_match($regexp, $headline);
}
Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • I take this as there is no such function in the PHP libraries. Yea, my next step was to do a function instead, but You already made one for me, thanks. I will modifie it to fit my purposes better, thank you. – Max Kielland May 08 '11 at 13:27
  • You are welcome. If you know exactly what are the possible values within the $Fields array (eg. not empty and consisting only from lowercase and uppercase letters) and that the number of elements in $Fields array won't be very big, you can just construct regular expression (eg. [allowing case-insensitive matches](http://pl.php.net/manual/en/reference.pcre.pattern.modifiers.php)) and use it within [preg_match()](http://pl.php.net/manual/en/function.preg-match.php) function. I believe it may give you better performance. – Tadeck May 08 '11 at 13:33
  • I think the reges is better with `$regexp = '~(' . implode( '|', array_values( $array ) ) . ')~iU';`. This works also with strings are `/` and count values. – bueltge Mar 21 '13 at 09:27
  • 1
    @bueltge: This comes to deciding what kind of values we could expect within `$array`, and this becomes more complex. First of all, you could possibly see some values containing `~` also. Secondly, the values themselves may contain some [meta characters](http://www.php.net/manual/en/regexp.reference.meta.php), especially some [escape sequences](http://www.php.net/manual/en/regexp.reference.escape.php). That second case should be addressed with [`preg_quote()`](http://www.php.net/manual/en/function.preg-quote.php) or something similar - it will escape special characters. – Tadeck Mar 21 '13 at 09:58
1

http://www.php.net/manual/en/function.array-search.php that's what you looking for

example from php.net

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>
afarazit
  • 4,907
  • 2
  • 27
  • 51
  • Sorry, I initially downvoted because your original answer was just a link to the array search docs with no explanation of how to use it. It's locked in now. – Wesley Murch May 08 '11 at 13:15
  • The down vote is not from me, but consider a function like this: array_in_str("My long string with many words",array("word","number")); This will get a match because "word" is a part of "words". – Max Kielland May 08 '11 at 13:16
  • alright, check our `array_search()` i think that's the function you looking fore – afarazit May 08 '11 at 13:17
  • @wesley you can remove the downvote if @atno makes an edit. The edit has to be made about 5 minutes after the post though. It won't count otherwise – JohnP May 08 '11 at 13:27
  • @JohnP: It won't let me because the post was edited during the grace period. – Wesley Murch May 08 '11 at 13:34
  • This doesn't answer the question. array_search searches an array for a value, it does not search a string for values found in a array. – Dom Dec 14 '16 at 15:19