SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Remove A HREF from some text ?

    Umm... Seems like a trivial matter doesn't it ?

    Proberly been dealt with before, though how do I remove a LINK from some text; not very good at Reg. Exp. folks... It's just a typical link really.

    PHP Code:
    ...text... <a href='http://www.myaddress.com'>...</a> ...more text...

    // and this

    ... <a href='www/myaddress.com'>...</a> ... 
    Any help would be welcome, as always.

    Dr Livingstone.

  2. #2
    SitePoint Zealot Sork's Avatar
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tryed this and it worked
    PHP Code:
    $text preg_replace('/<a(.*?)a>/'''$text); 
    Maybe some expert will give you a better expression

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2003
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, if you want to keep the anchor text try:

    PHP Code:
    $text preg_replace('#<a.*?>(.*?)</a\s*>#is'"\$1"$text); 
    # - used here as pattern delimiter
    .*? - non-greedy matching
    \$1 - back-reference to the first expression between ()
    \s* - matches any numbery of blanks

    ALso used pattern modifiers:
    i - case insensitive
    s - dot (.) also matches newline carachters

    Did i forget something?

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks folks, I'll check them out and get back to you...

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RU Serious - Cheers mate Worked wonders... Thanks for the tips btw;

    Sork - Thanks as well

  6. #6
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Slap me if I've missed the point but wouldn't strip_tags() help? Ok - it works on you giving it a list of allowed tags but it makes life easy.

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm... Not sure Harry, something to look at ? Although I found this at the page you have;

    PHP Code:
    To fix the <scr<script></script>ipt> bug, I'm going to use the following function for my upcoming forum project:

    function realsafehtml($str) {
    // Don't do anything if there's no difference or if the original string is empty
    $oldstr = "";

    while($str != $oldstr) // Loop until it got no more effect
    {
    $oldstr = $str;
          //nuke script and header tags and anything inbetween
          $str = preg_replace("'<script[^>]*?>.*?</script>'si", "", $str);
          $str = preg_replace("'<head[^>]*?>.*?</head>'si", "", $str);
          
          //listed of tags that will not be striped but whose attributes will be
          $allowed = "br|b|i|p|u|a|center|hr";
          //start nuking those suckers. don you just love MS Word's HTML?
          $str = preg_replace("/<((?!\/?($allowed)\b)[^>]*>)/xis", "", $str);
          $str = preg_replace("/<($allowed).*?>/i", "<\\1>", $str);
    }

          return $str;
    }

    It's obviously more expensive than the original function, but also a lot more secure :) I hope this can help someone - didn't test it much yet, though. 
    ...

  8. #8
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just removing? I expect this would do it:
    PHP Code:
    $replace preg_replace('/<\/?a.*?>/s''' $source); 
    Two steps might be faster:
    PHP Code:
    $replace preg_replace('/<a.*?>/s''' $source);
    $replace str_replace('</a>'''$replace); 
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    Sure have a look...

  10. #10
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another alternative to regular expressions would be PEAR::HTMLSax (for which I'm partly guilty ) - you'd could parse the incoming post and filter out extactly the tags/attributes you don't want.

  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Harry, although I must say that PEAR has improved a lot recently, I still ain't too keen to use it; though I'd proberly be interested in the package whch generates TABLEs ? If I was to use PEAR that is But not at the moment... It's getting there, but IMO to get it really moving it needs some backing from a PHP body/person if you see what I mean ? Thanks all the same though


Bookmarks

Posting Permissions

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