Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regular expresions to detect and remove links

Posted on 2012-03-29
3
Medium Priority
?
346 Views
Last Modified: 2012-03-29
So I want to do some in php, I get a text from wordpress that contains certain links, so what I want to do is first get all the files matching the following

http://www.anydomain.tdl/wp-content/uploads/somefolder/somefolder/filename.ext

And get a list of all the files on that text matching certain extensions. So basically the stuff in bold will repeat in every link I need to match, but the other thing will change, and I need a place to define the list of extensions for ext.

After that I need to remove from the text the matching link, meaninng:

<a href="matched_link" something> something</a>

Can some body help me with this.
0
Comment
Question by:brauliomendez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 37785214
Ok, something like this should match your links:

preg_match_all('#http://www\.(?:[a-z\d\-]*[a-z\d]\.)+[a-z]{2,}/wp-content/uploads/[^/"\'\s?&]+/[^/"\'\s?&]+/[^/"\'\s?&]+\.(?:jpg|gif)#',$sourcestring,$matches);
print_r($matches);

Open in new window


I'm a little unclear about what you want done for the big picture though. If you had text:

blah blah <a href="matched_link" something> something</a> blah

Did you want your result to be:
blah blah blah

?
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 2000 total points
ID: 37785228
If I understood your requirement correctly, this should do the trick:
preg_replace('#<a\s[^>]*?href\s*?=\s*?(\'|")http://www\.(?:[a-z\d\-]*[a-z\d]\.)+[a-z]{2,}/wp-content/uploads/[^/"\'\s?&]+/[^/"\'\s?&]+/[^/"\'\s?&]+\.(?:jpg|gif)\1[^>]*>.*?</a>#is','',$sourcestring);

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 37785230
You should be able to see how I've allowed for multiple extensions by example. In my case I allowed jpg and gif. You can expand the list like this if you have an array of extensions:
$extensions = array("gif","jpg","etc");
$extensionPattern = implode("|",$extensions);
preg_replace('#<a\s[^>]*?href\s*?=\s*?(\'|")http://www\.(?:[a-z\d\-]*[a-z\d]\.)+[a-z]{2,}/wp-content/uploads/[^/"\'\s?&]+/[^/"\'\s?&]+/[^/"\'\s?&]+\.(?:'.$extensionPattern.')\1[^>]*>.*?</a>#is','',$sourcestring);

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question