Removing html anchors from POST'ed text string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dariusz

    Removing html anchors from POST'ed text string

    I am having problems with trying to remove some tags using PHP. What I
    want is to perform a search and replace on a URL, so if someone tried to
    put a URL into a guestbook database, instead of the user inputting a
    link as:

    <a href="http://whatever.com">v isit me</A>

    It goes into the database as:



    I have tried the str_replace() function, but doesn't seem to work. I have
    also tried strip_tags() but then that just give the result:

    visit me

    This is the code I've written.

    $AmendedComment = str_replace("<a href=\"", " ", $_POST['Comment']);
    $FinalComment = addslashes($Ame ndedComment);

    Checking the database and reading it out again, the first part of the URL
    remains intact, so the URL is clickable - not what I want.

    I've made an attempt at preg_replace() but it's difficult to get head
    around trying to write a pattern.

    Any help appreciated.

    Dariusz
  • Jan Pieter Kunst

    #2
    Re: Removing html anchors from POST'ed text string

    In article <Riqkc.36277$h4 4.5382158@stone s.force9.net>,
    ng@lycaus.plusY OURSHIT.com (Dariusz) wrote:
    [color=blue]
    > I am having problems with trying to remove some tags using PHP. What I
    > want is to perform a search and replace on a URL, so if someone tried to
    > put a URL into a guestbook database, instead of the user inputting a
    > link as:
    >
    > <a href="http://whatever.com">v isit me</A>
    >
    > It goes into the database as:
    >
    > http://whatever.com[/color]

    [...]
    [color=blue]
    > I've made an attempt at preg_replace() but it's difficult to get head
    > around trying to write a pattern.[/color]


    First attempt (I checked that it works on your example, I didn't try
    anything else):

    $href = '<a href="http://whatever.com">v isit me</A>';
    $unclickable_ur l = preg_replace('! <a.+?href="([^"]+)".*?</a>!i', "$1",
    $href);
    echo $unclickable_ur l;

    I'm sure this is not good enough yet. Feel free to improve, anyone.

    JP

    --
    Sorry, <devnull@cauce. org> is een "spam trap".
    E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

    Comment

    Working...