www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] clean links with regular expression

  1. #1
    Join Date
    Sep 2006
    Posts
    213

    resolved [RESOLVED] clean links with regular expression

    Using PHP5

    I'm trying to clean the garbage 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" id="" title="" target="" onclick="" style="" onmousedown="UntrustedLink.bootstrap($(this), "7AQBFjCZX", event, bagof({}));" rel="nofollow"><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" id="" target="_blank" style="" onmousedown="UntrustedLink.bootstrap($(this), "3AQCzlYSy", event, bagof({}));" rel="nofollow">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" id="" title="" target="" onclick="" style="" onmousedown="UntrustedLink.bootstrap($(this), "oAQCL4kU9", event, bagof({}));" rel="nofollow"><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" id="" target="_blank" style="" onmousedown="UntrustedLink.bootstrap($(this), "ZAQB5UkKU", event, bagof({}));" rel="nofollow">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>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,304
    Maybe something like this:
    PHP Code:
    $regexp '#<a[^>]+href\s*=\s*[\'"](.*?)[\'"][^>]*>#i';
    $string preg_replace($regexp'<a href="$1">'$string); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2006
    Posts
    213
    sweet, thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles