SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Rocket Scientist
    Join Date
    Jun 2003
    Location
    Manchester, UK
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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.

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2006
    Location
    Gold Coast, Australia
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you please post the code for the array in question and all related code.

  3. #3
    Rocket Scientist
    Join Date
    Jun 2003
    Location
    Manchester, UK
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 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 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 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" />

    ------


    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.

  4. #4
    SitePoint Member somnambul's Avatar
    Join Date
    May 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $founded = array();
    foreach ( 
    $anArray as $key => $value ) {
       if( 
    substr_count($value'main.jpg ') > ) {
            
    $founded[] = $value;
       }
    }

    print_r($founded); 

  5. #5
    Rocket Scientist
    Join Date
    Jun 2003
    Location
    Manchester, UK
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!!!

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

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

  6. #6
    SitePoint Member somnambul's Avatar
    Join Date
    May 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ian Cunningham View Post
    Thanks!!!

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

    Code:
    foreach ( $venueimage as $key => $value ) {
     if( substr_count($value, 'main.jpg') > 0 ) {
     $venueimage = $value;
     }
    }
    like that you will overwrite the $venueimage with last found result(match)

  7. #7
    Rocket Scientist
    Join Date
    Jun 2003
    Location
    Manchester, UK
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, that's ok, I don't need the rest of the array, just that result .

    Thanks for your help .


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •