Welcome to WebmasterWorld Guest from 54.145.209.107

Forum Moderators: coopster & jatar k

Urgent Regular Expression help needed

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

5+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member



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)

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



>> 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)

10+ Year Member



Isn't this simpler?

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

Featured Threads

Hot Threads This Week

Hot Threads This Month