470,833 Members | 1,576 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

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">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
Jul 17 '05 #1
1 1647
In article <Ri*********************@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, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Busy | last post: by
2 posts views Thread by johkar | last post: by
2 posts views Thread by charlie_M | last post: by
4 posts views Thread by Ahmed, Shakir | last post: by
reply views Thread by Adam Pletcher | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.