Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    135
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Preg Match/Regular expressions question.

    I'm trying to pull the image url out of this string:
    <img src="http://theimagehost.com/theimage.jpg"></a>



    preg_match("'<img src=(.*?)></a>'si", $finalArray[$plusOne], $out_imageinfo);
    list($width, $height) = getimagesize("$out_imageinfo[0]");


    Warning: getimagesize(<img src="http://theimagehost.com/theimage.jpg"></a>)

    Obviously I'm getting the entire string... I just want the url. I have been trying to figure it out with: http://www.regular-expressions.info/, but no luck yet...


    I'm trying to use...
    preg_split("'<img src=(.*?)></a>'si", $finalArray[$plusOne], $out_imageinfo);

    It won't do anything?
    Last edited by iceflyin; 08-09-2007 at 12:09 AM.

  • #2
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yeah that's because your splitting the whole instance of <img> out, here is an example:
    Code:
    <?php
    $image = "<img src=\"picture.jpg\" />";
    list($tag, $img, $tag2) = split("[\"]+", $image, 3);
    echo $img;
    ?>
    the output is picture.jpg and if it is in a directory it will show the directory aswell

    what I did is to go through the string $image and split the string each time it found a "

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Posts
    135
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Actually, that doesn't help... Because I'm trying to strip it out of string that contains multiple URLSs of different types.

    However, the string only contains one instance of: "jpg"


    So, what I'm trying to do is more like: REGEX: "^<(http)[*?](.jpg)/si" - Try to find a string that starts with http, and ends in JPG, and doesn't contain any <'s.

    But, I can't get it to work at all. I'm new to RegEX.

    I can't get this to do anything other than echo ArrayArrayArrayArray.........

    Code:
            preg_match_all("/'http'(.+?)'jpg'/", $image, $matches);
            $array = $matches[2];
            echo $matches[0];
            echo $matches[1];
            echo $matches[2];
            echo $array[0];
            echo $array[1];
            echo $array[2];
    And, here is an actual string I'm trying to parse and image out of...
    Code:
    <?php
    $data = "<a href=\"http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendid=3180342\" id=\"ctl00_Main_ctl00_UserFriends1_FriendRepeater_ctl02_friendLink\">Jeff</a><br><a href=\"http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendid=3180342\" id=\"ctl00_Main_ctl00_UserFriends1_FriendRepeater_ctl02_friendImageLink\"><img src=\"http://a267.ac-images.myspacecdn.com/00220/66/23/220403266_s.jpg\">" ;
    $regex = '^</http(.+?)jpg/si';
    preg_match($regex,$data,$match);
    var_dump($match);
    echo $match[1];
    ?>
    Last edited by iceflyin; 08-10-2007 at 09:35 AM.

  • #4
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    try this
    PHP Code:
    <?php
    $image 
    "<img src=\"http://sumurl.com/picture.jpg\" />";
    preg_match("<[a-zA-Z0-9_-]*.jpg>"$image$matchPREG_OFFSET_CAPTURE1);
    //print_r($match);
    $image_array $match[0];
    $image_name $image_array[0];
    echo 
    $image_name;
    ?>
    and if it is multiple urls in a string why dont u split them up then do a foreach loop and run them through the preg_match to extract the image name?

  • #5
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    srry i double posted

  • #6
    Regular Coder
    Join Date
    Jul 2007
    Posts
    135
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I just got this to work, but it cuts out the "jpg" part, hows it doing that? Oh wait, Ic... Hum... Ill just append "jpg", that works...

    Code:
    $regex = '/<img src=\"([^\"]*)(jpg)/iU';
    preg_match_all($regex,$data,$match);
    var_dump($match);
     $array = $match[1];
    echo "<br><br> $array[0]";
    Last edited by iceflyin; 08-10-2007 at 10:11 AM.


  •  

    Posting Permissions

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