For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > April 2004 > Removing html anchors from POST'ed text string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Removing html anchors from POST'ed text string
Dariusz

2004-04-30, 9:18 am

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">visit me</A>

It goes into the database as:

http://whatever.com

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($AmendedComment);

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

2004-04-30, 9:18 am

In article <Riqkc.36277$h44.5382158@stones.force9.net>,
ng@lycaus.plusYOURSHIT.com (Dariusz) wrote:

> 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">visit me</A>
>
> It goes into the database as:
>
> http://whatever.com


[...]

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



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

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

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" = @.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com