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 7 of 7
  1. #1
    nfn
    nfn is offline
    New Coder
    Join Date
    Dec 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regular Expressions

    Hi,

    I'm not a regex man, so I need some help to make a search & replace!
    I get from a table an article body and I want to find any anchor that as an image tag and apply a specific css class to the anchor;

    Reading some tutorials I already reach something that I think it works, but I need to replace the string.

    PHP Code:
    $a '<a href="image/test.gif"><img src="image/test.gif" /></a>';

    if (
    preg_match("/^(<a href.*?<img src.*?<\/a>)$/"$a$matches)) {
      echo 
    "Match was found <br />";
      echo 
    htmlspecialchars($matches[0])."<br>";


    I need to find '<a href' AND '<img src' AND '</a>' ... if it finds this pattern then should replace '<a href' with '<a href class="abcd"'.

    Thanks

    N

    Edited: I forgot to mention that the image must be the same in the anchor and in the image tag!
    Last edited by nfn; 01-16-2008 at 02:30 PM.

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Try this:

    PHP Code:

    $text 
    '<a href="image/test.gif"><img src="image/test.gif" /></a>';

    $text preg_replace'#<a(.*?)>(.*?<img.*?)</a>#is''<a$1 class="abcd">$2</a>'$text );

    echo 
    $text
    Outputs:

    Code:
    <a href="image/test.gif" class="abcd"><img src="image/test.gif" /></a>

  • #3
    nfn
    nfn is offline
    New Coder
    Join Date
    Dec 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That works ...

    It is possible to match the image file 'path/name'.
    It needs to be the same in the anchor and in the img tag.

    This class only applies to images!

    Thanks, N

  • #4
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Ok, try this instead:

    PHP Code:
    $text preg_replace'#<a(.*?href="(.*?)".*?)>(.*?<img.*?src="\2".*?)</a>#is''<a$1 class="abcd">$3</a>'$text ); 

  • #5
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    PHP Code:
    $a '<a href="image/test.gif"><img src="image/test.gif" /></a>';
    $text preg_replace(
        
    '/<a href="([^"]+)">\w*<img src="\1" ?\/>\w*<\/a>/'
        
    '<a href="$1"><img class="abcd" src="$1"/></a>'
        
    $a);
    echo 
    $text 
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #6
    nfn
    nfn is offline
    New Coder
    Join Date
    Dec 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Every sample works very well, but none match the image path and name!

    <a href="image/test.gif"><img src="image/test.gif" /></a>

    Bold anchor and src must be the same!

  • #7
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    In both our examples they do have to be exactly the same, try changing the src or href and the regex won't match.


  •  

    Posting Permissions

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