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.

2
  • Ok, some questions: 1) what are possible values within $Fields array? 2) Do you need case insensitive search? Commented May 8, 2011 at 13:27
  • Case insensitive and the values are user inputs (say no more :) Commented May 8, 2011 at 14:05

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
I think the reges is better with $regexp = '~(' . implode( '|', array_values( $array ) ) . ')~iU';. This works also with strings are / and count values.
@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, especially some escape sequences. That second case should be addressed with preg_quote() or something similar - it will escape special characters.
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;
?>

6 Comments

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.
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".
alright, check our array_search() i think that's the function you looking fore
@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: It won't let me because the post was edited during the grace period.
|

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.