homepage Welcome to WebmasterWorld Guest from 54.197.19.35
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Urgent Regular Expression help needed
tabish




msg:4278326
 1:45 pm on Mar 8, 2011 (gmt 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

 

timster




msg:4278620
 2:23 am on Mar 9, 2011 (gmt 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.

eelixduppy




msg:4278660
 4:21 am on Mar 9, 2011 (gmt 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...]

rominosj




msg:4278706
 8:11 am on Mar 9, 2011 (gmt 0)

Isn't this simpler?

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved