SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression Help!

    Hi guys and gals - looking for a little help here if possible...

    I have the following string stored in a database field:-

    Code:
    <img src="/images/foldername/image.gif" class="absmiddle" alt="Image">
    Now, I have pulled that out of the database successfully, but I need to find a way of pulling just the image name (or src="" substring) out of the string. I assume some sort of regular expression would be needed here, but I just can't get my head around it.

    Also, I need to be able to replace the whole string (from "<img" to ">") with something else for display purposes. Don't ask! Is there an easy regular expression that will allow me to do this?

    Thanks in advance all, Regular Expressions are really confusing me.

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first:

    PHP Code:
    <pre>
    <?php

    $email 
    '<img src="/images/foldername/image.gif" class="absmiddle" alt="Image">';

    preg_match('#src="(.+?)"#is'$email$match);

    print_r($match);

    ?>
    </pre>
    The second (only works if there is just the image markup and nothing else)

    PHP Code:
    <?php

    $email 
    '<img src="/images/foldername/image.gif" class="absmiddle" alt="Image">';

    echo 
    preg_replace('#<[^>]*>#s''something else'$email);
    ?>

  3. #3
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do this with substr.
    PHP Code:
    $string substr($string4, -1); 
    See how that works?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


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
  •