Search Array for substring and return string

I’m a real PHP novice, so please excuse any incorrect terminology :).

I’ve got some HTML code containing text and a set of a few images, where I need to isolate and return a small string containing a particular image.

I’ve cleaned up the HTML to remove all text and then inserted this data in to an array using explode().

One of the array variables has the text “main.jpg” in it and I need to return the whole array value. The image name is a set of random characters, but always has main.jpg somewhere in the name. I’ve looked up all sorts of methods, using preg_grep etc…, and I’m either doing it incorrectly or don’t know the correct function.

The question is, how can I check an array for a substring and then return the whole value?

Any help would be most welcome. I’ve been trying to get this coded for a few hours, but as I’m unfamiliar with PHP it’s a real challenge.

Can you please post the code for the array in question and all related code.

Sure, the array looks like this (that’s the 3 values I’m using in this example):

------

<img src="http://www.bigday.co.uk/uploads/monthly_06_2010/ccs-1-069633500[/SIZE][SIZE=2] 1276273586_thumb.jpg" id=‘ipb-attach-img-145-0-65465900 1276605659’ style=‘width:120;height:90’ class=‘attach’ width=“120” height=“90” alt=“Attached Image: int_03.jpg” />

------

<img src="http://www.bigday.co.uk/uploads/monthly_06_2010/ccs-1-057034300[/SIZE][SIZE=2] 1276273615_thumb.jpg" id=‘ipb-attach-img-148-0-65814500 1276605659’ style=‘width:120;height:90’ class=‘attach’ width=“120” height=“90” alt=“Attached Image: scen_10.jpg” />

------

<img src="http://www.bigday.co.uk/uploads/monthly_06_2010/ccs-1-077508800[/SIZE][SIZE=2] 1276604501_thumb.jpg" id=‘ipb-attach-img-170-0-65826600 1276605659’ style=‘width:120;height:90’ class=‘attach’ width=“120” height=“90” alt=“Attached Image: main.jpg” />

[SIZE=2]------

[/SIZE]
As you can see the last array value has “main.jpg” in it, and I need to make that whole value display wherever I echo $venueimage.


$founded = array();
foreach ( $anArray as $key => $value ) {
   if( substr_count($value, 'main.jpg ') > 0 ) {
        $founded[] = $value;
   }
}

print_r($founded);

Thanks!!! :cool:

I got it working by modifying that code slightly, but now it works! :smiley:

[SIZE=2][COLOR=black]

foreach ( $venueimage as $key => $value ) {
 if( substr_count($value, 'main.jpg') > 0 ) {
 $venueimage = $value;
 }
}

[/COLOR][/SIZE]

like that you will overwrite the $venueimage with last found result(match)

Yep, that’s ok, I don’t need the rest of the array, just that result :).

Thanks for your help :D.