Welcome to WebmasterWorld Guest from 54.167.111.180

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Urgent Regular Expression help needed

     
1:45 pm on Mar 8, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Aug 22, 2005
posts: 323
votes: 0


Hello All,

One of my wordpress site is hacked and in my posts, some links are inserted.

Now if I try to search HREF tag using regular expressions, all the anchor tag around image tag is also getting removed.

So what I want is:

Remove anchor tag which has no image inside: for example

<a href="example.com">any example text</a> This needs to be removed

but this anchor should not be removed: <a href="example.com"><img src=""></a>

I am very bad in RegExp, so can anyone help me.. pls?

Regards
2:23 am on Mar 9, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 1, 2003
posts:815
votes: 0


I told myself I was going to stop writing people's regexes for them, but OK, here you go:

$a = preg_replace('/\<a href\=\"[\w\.]+\"\>\w[^<]+\<\/a>/i','',$a);


Bear in mind this doesn't come with any guarantees, and it will probably wreck your data and make your car's paint peel, so test and back up everything before you think about using it.

The gist of the regex is that it looks for a word characters beginning the anchor text, and won't match the "<" that begins the links with images inside, which you said you wanted to keep.

You didn't mention how you wanted whitespace to be handled outside the links, so this regex doesn't make any changes to that.
4:21 am on Mar 9, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


>> I told myself I was going to stop writing people's regexes for them, but OK, here you go

haha,I think someone likes to write regex ;)

>> I am very bad in RegExp

The only way to get better at writing them is by forcing yourself to write them yourself; most likely you won't get anything close to what you want until you play around with it a bunch of times, but once you master regex you will be able to write them like they're nothing.

Here's a good place to start: [php.net...]
8:11 am on Mar 9, 2011 (gmt 0)

Junior Member from PE 

10+ Year Member

joined:Mar 28, 2004
posts: 141
votes: 0


Isn't this simpler?

<([A-Z][A-Z0-9]*)[^>]*>(.*?)\w</\1>
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members