Only replace own website url in text with HTML links

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
richprich
Forum Newbie
Posts: 1
Joined: Thu Mar 15, 2012 9:25 pm

Only replace own website url in text with HTML links

Post by richprich »

When user post a comment, i strip all the html tags in the comment before insert into my database because i do not want them to post external links(SPAM) in the comment. But i only want to strip external links, i want to display my own website URL as normal clickable links. How to detect the URL is my own website's URL and make it clickable as usual?

Example in the comment: Blah... blah... blah... http://my_website_url.com Blah... blah... blah... Blah... blah... blah... Blah... blah... blah... Blah... blah... blah... Blah... blah... blah...http://external_links.com Blah... blah... blah... Blah... blah... blah...

As above example, i want ONLY http://my_website_url.com become clickable link before insert into my database. (http://my_website_url.com to < a href="http://my_website_url.com" >http://my_website_url.com< /a > )

Also, not only detect the main URL:http://my_website_url.com, but also:

-http://www.my_website_url.com

-www.my_website_url.com

-my_website_url.com

-http://my_website_url.com/blah/blah/blah

-http://www.my_website_url.com/blah/blah/blah

-www.my_website_url.com/blah/blah/blah

-my_website_url.com/blah/blah/blah

or any URLs that from my website:http://www.my_website_url.com/xxx/xxx/xxx/xxx

If could, please post exactly php codes to let me copy and paste into my file because i have little knowledge with php only. Thanks guys. :)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Only replace own website url in text with HTML links

Post by social_experiment »

To strip html tags use http://www.php.net/manual/en/function.strip-tags.php
richprich wrote:please post exactly php codes to let me copy and paste into my file because i have little knowledge with php only
I suggest reading a tutorial on regular expressions; there is a section viewforum.php?f=38 dedicated to this area and it also contains a two part tutorial which should help;
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply