/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] clean links with regular expression

[B]Using PHP5[/B]

I’m trying to clean the [COLOR=”Red”]garbage[/COLOR] out of links like the ones below with regular expression. Everything I’ve tried either removes too much or not enough. Some links have empty attributes, others don’t. Some have all of these attributes and more and yet others don’t. Any help with this would be much appreciated.

[CODE]
<div class=”post”>
<span class=”description”>Share the importance of prevention & public health in the lives of Americans. Add a button to your site.<br/><br/><a href=”http://www.cdc.gov/24-7/SocialMedia.html?s_cid=fb1055″ [COLOR=”Red”]id=”” title=”” target=”” onclick=”” style=”” onmousedown=”UntrustedLink.bootstrap($(this), “7AQBFjCZX”, event, bagof({}));” rel=”nofollow”[/COLOR]><img class=”img” src=”http://external.ak.fbcdn.net/safe_image.php?d=AQCsdry3J624lWbO&[/COLOR]w=90[COLOR=”DarkGreen”]&h=90&url=http%3A%2F%2Fwww.cdc.gov%2F24-7%2Fimages%2Fbutton_300x250.jpg” alt=”” /></a><br/><a href=”http://www.cdc.gov/24-7/SocialMedia.html?s_cid=fb1055″ [COLOR=”Red”]id=”” target=”_blank” style=”” onmousedown=”UntrustedLink.bootstrap($(this), “3AQCzlYSy”, event, bagof({}));” rel=”nofollow”[/COLOR]>CDC Works For You 24/7 – Social Media</a><br/>www.cdc.gov</span>

<span class=”date”>August 1st 2011, 19:07</span>
<span class=”link”><a href=”http://www.facebook.com/CDC/posts/243295989027701″>Link to status update</a></span>
</div>

<div class=”post”>
<span class=”description”>On National Girlfriends Day and all year round, take steps to be a healthy role model and help your girlfriends live safer and healthier lives. <br/><br/><a href=”http://www.cdc.gov/Features/GirlfriendsHealth/?s_cid=fb1053″ [COLOR=”Red”]id=”” title=”” target=”” onclick=”” style=”” onmousedown=”UntrustedLink.bootstrap($(this), “oAQCL4kU9″, event, bagof({}));” rel=”nofollow”[/COLOR]><img class=”img” src=”http://external.ak.fbcdn.net/safe_image.php?d=AQBAmjsSepDhIGQm&w=90&h=90&url=http%3A%2F%2Fwww.cdc.gov%2FFeatures%2FGirlfriendsHealth%2FGirlfriendsHealth_355px.jpg” alt=”” /></a><br/><a href=”http://www.cdc.gov/Features/GirlfriendsHealth/?s_cid=fb1053″ [COLOR=”Red”]id=”” target=”_blank” style=”” onmousedown=”UntrustedLink.bootstrap($(this), “ZAQB5UkKU”, event, bagof({}));” rel=”nofollow”[/COLOR]>Girlfriends’ Health</a><br/>www.cdc.gov<br/>On National Girlfriends Day and all year round, start and maintain traditions that include healthy behaviors to help you and your girlfriends live safer and healthier lives.</span>
<span class=”date”>August 1st 2011, 15:43</span>
<span class=”link”><a href=”http://www.facebook.com/CDC/posts/227425243960555″>Link to status update</a></span>
</div>
[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 02.2011 — Maybe something like this:
[code=php]
$regexp = '#<a[^>]+hrefs*=s*['"](.*?)['"][^>]*>#i';
$string = preg_replace($regexp, '<a href="$1">', $string);
[/code]
Copy linkTweet thisAlerts:
@bustyaauthorAug 02.2011 — sweet, thanks
×

Success!

Help @bustya spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...