Solved

Regular expresions to detect and remove links

Posted on 2012-03-29
3
322 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
  • 3
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now