How to find the key from an array of strings with a certain substring

I apologize right out of the gate if this is the wrong forum… typical excuse, I’m new here.

If anyone can help with this, I’ll be so thankful! I’ve wrestled with this longer than I care to mention, and Google is probably eyeing me for so much bandwidth usage from searching for what I am sure should be a simple task in PHP!

Objective: Creating a PHP function that reads a remote HTML file, explodes it into an array of HTML strings based on newlines, finds the key from said array of strings that contains a particular phrase, then returns the key number into a variable – so I can then explode that particular string, strip the HTML tags, and return only the portion of that string I want to then display with my script.

I have accomplished reading the remote file, exploding into an array of strings based on newlines, manually finding the correct array key, exploding the string, stripping the tags, and returning the portion of the string I want.

What I can’t seem to figure out is how to make my function search through the array of strings, find a particular phrase in one of those strings (not the full string, obviously), and return the key for that string.

Example: I have an array thus:

0 => I have a cat.
1 => My dog is blue.
2 => One cute hamster.

How in the world can I take that array, search it for “dog” and have PHP return “1” so I can assign that key number to a variable?

I have tried a plethora of approaches including in_array, array_search, and foreach key/value loops followed with in_array, array_search, etc with no success. Any input would be mucho appreciated-o.

Thanks! :drink:

<?php
$my_array = array('one' => 'I have a cat.', 'two' => 'My dog is blue.', 'three' => 'One cute hamster.');
$search_term = "dog";

function search_array($array, $term)
{
	foreach ($array AS $key => $value) {
		if (stristr($value, $term) === FALSE) {
			continue;
		} else {
			return $key;
		}
	}

	return FALSE;
}

var_dump(search_array($my_array, $search_term));
?>

Not multi-dimensional friendly but could be made so easily.

I would use array_filter for that but…

What is the html you’re trying to find? Sounds like you could use regexp.

Just to simplify (abit…), simshaun’s code.


function search_array ( array $array, $term )
{
    foreach ( $array as $key => $value )
        if ( stripos( $value, $term ) !== false )
            return $key;

    return false;
}

Or use array_filter as sugested.


$term = 'dog';
$ser = function ( $val ) use ( $term ) {
    return ( stripos( $val, $term ) !== false ? true : false );
};
$keys = array_keys( array_filter( $my_array, $ser ) );

The above is using PHP 5.3 features.

@simshuan: That logic worked perfectly! Thank you!

@php_daemon: I’m not looking for certain HTML, but a certain line within the entire HTML “page” in order to extract a piece of data from a single string of HTML. How could I do this with regexp, for future reference???

@logic_earth: I did use your first simplified reference, but with ===, not !==. Thanks for your help!!! :slight_smile:

Apparently, I had my logic backwards: I was searching based on “if it’s true, return” instead of “if it’s false, keep moving, else return.”

I owe all of you a cold beer… where can I fax one? :beer:

That’s what I meant, and asked because I wanted to have a real sample.

For example if you were looking for the images in the html:


preg_match('#<img src="(.+?)"#is', $html, $matches);

print_r($matches);

If you’re wanting to use regular expressions, you can use preg_grep. For example:


$lines   = array('I have a cat.', 'My dog is blue.', 'One cute hamster.', 'Dogs are cool');
$matches = preg_grep('/\\bdogs?\\b/i', $lines);
$keys    = array_keys($matches);

var_dump($matches);