PHP Array values ​​in a string?

I searched the PHP manual for a while and cannot find any command that does what I want.

I have an array with keys and values, for example:

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

Then I have a line, for example:

$Headline = "My black coffee is cold";

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

Example:

Array_function_xxx($Headline,$Fields);

Gives the result true because "bl" is in the $ Headline line (as part of "Black").

I ask because I need performance ... If this is not possible, I will just make my own function ...

EDIT . I am looking for something like stristr (string $ haystack, array $ needle);

thank

SOLUTION . I came up with its 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;
}

, .

+5
2

:

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
}

, .

EDIT:

, (, , , $fields):

function Array_function_xxx($headline, $fields) {
    $regexp = '/(' . implode('|',array_values($fields)) . ')/i';
    return (bool) preg_match($regexp, $headline);
}
+5

http://www.php.net/manual/en/function.array-search.php

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;
?>
+1

All Articles