1

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. :)

0

1 Answer 1

2

I wrote a function for it:

function makeSiteClickable($dom, $website = 'my_website_url.com')
{
    $words = explode(' ', $dom);
    foreach($words as &$word){
        if(strpos($word, $website) == true){
            // there you can make some sterilizing
            $href = str_replace('http://', '', $word);
            $href = str_replace('www.', '', $href);
            $word = '<a href="http://'.urlencode($href).'">'.$word.'</a>';
        }
    }

    return implode(' ', $words);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks seferov, but the code is not work if there is a comma or line break after the word. Example: This is a test: my_website_url.com, and blah blah blah... as above example, it will echo: < a href="my_website_url.com,">my_website_url.com,< /a> or if there is line break after the my_website_url.com, it will echo: < a href="my_website_url.com<br><br>this">my_website_url.com<br><br>this< /a>.
I just give the basic structure of code. You may such characters to an array and explode the whole file accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.