Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-16-2008, 02:18 PM   PM User | #1
nfn
New Coder

 
Join Date: Dec 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
nfn is an unknown quantity at this point
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..
nfn is offline   Reply With Quote
Old 01-16-2008, 02:51 PM   PM User | #2
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
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>
Mwnciau is offline   Reply With Quote
Old 01-16-2008, 02:58 PM   PM User | #3
nfn
New Coder

 
Join Date: Dec 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
nfn is an unknown quantity at this point
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
nfn is offline   Reply With Quote
Old 01-16-2008, 03:10 PM   PM User | #4
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
Ok, try this instead:

PHP Code:
$text preg_replace'#<a(.*?href="(.*?)".*?)>(.*?<img.*?src="\2".*?)</a>#is''<a$1 class="abcd">$3</a>'$text ); 
Mwnciau is offline   Reply With Quote
Old 01-16-2008, 03:17 PM   PM User | #5
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
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
shyam is offline   Reply With Quote
Old 01-16-2008, 03:25 PM   PM User | #6
nfn
New Coder

 
Join Date: Dec 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
nfn is an unknown quantity at this point
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!
nfn is offline   Reply With Quote
Old 01-16-2008, 06:09 PM   PM User | #7
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
In both our examples they do have to be exactly the same, try changing the src or href and the regex won't match.
Mwnciau is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:08 PM.


Advertisement
Log in to turn off these ads.