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.

share|improve this question
Ok, some questions: 1) what are possible values within $Fields array? 2) Do you need case insensitive search? – Tadeck May 8 '11 at 13:27
Case insensitive and the values are user inputs (say no more :) – Max Kielland May 8 '11 at 14:05

2 Answers

up vote 3 down vote accepted

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);
}
share|improve this answer
Yep, that's the solution – maxedison May 8 '11 at 13:22
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 8 '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) and use it within preg_match() function. I believe it may give you better performance. – Tadeck May 8 '11 at 13:33

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;
?>
share|improve this answer
uh oh, why the down vote? – afarazit May 8 '11 at 13:09
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 8 '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 8 '11 at 13:16
alright, check our array_search() i think that's the function you looking fore – afarazit May 8 '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 8 '11 at 13:27
show 2 more comments

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.