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

Hmm, interesting. It's going to require some work but it can be done.

Here's how I would isolate your website's link out of the entire comment

// User's comment
$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...";

// Use strstr function to detect your domain in the comment.  Be careful not to put http:// or www. in front of your domain here, and it'll work for every variation the user puts in
$mydomain = strstr($comment, "my_website_url.com");

// Since url's have no spaces, isolate your url with explode function by detecting a space
$mydomainlink = explode(" ", $mydomain);

// And voila, here's your url, you can now format that into a link and insert into db
echo $mydomainlink[0];

Hi,

Thanks. But I tried the code. It works, but if the domain has a comma or line break, the link will echo "my_website_url.com," or "my_website_url.com<br><br>" or "my_website_url.com.". Any solutions?

Thanks..

Ouch, your links have commas and line breaks? That's rough buddy. Regardless, commas should work since you're using the explode function on a space.

Did you isolate the url BEFORE running the comment through strip tags?

Just some extra feedback. Did you do anything to change your query strings to commas instead of the "&" or is that just how the server is set up?

99.9999% of the time, you'll see the standard bla.com/thing=one&thing=two&thing=three

instead of the commas.

Member Avatar for diafol

Use preg replace?

Ouch, your links have commas and line breaks? That's rough buddy. Regardless, commas should work since you're using the explode function on a space.

Did you isolate the url BEFORE running the comment through strip tags?

I think he means the person posting the comment puts a comma right after the web link

You could explode on a few characters to help get rid of common ones

$chars = array(' ','<br>','<br/>',',','!',"\r","\n");
$mydomainlink = explode($chars, $mydomain);

But you still hit a problem with full stops and question marks cause they are in the url you can't explode on them, you could trim a fullstop or question mark off the end if there is one though as there is never any need for it to be the last char

$chars = array(' ','<br>','<br/>',',','!');
$mydomainlink = explode($chars, $mydomain);

$lastchardelete = array('?','.');
if(in_array(substr($mydomainlink,-1),$lastchardelete)){
	$mydomainlink = substr($mydomainlink,0,-1);
}

Thanks guys, I tested all above examples with many ways, but not really working to me and i want it to insert into database with whole comment after replace the links. I have the code below, it will replace my URLs to links. But, the problems are same. Please see the output below after the code:

<?
$comment = "Blah... blah... blah... http://my_website_url.com, Blah... blah... http://my_website_url.com<br><br>Blah... blah...http://my_website_url.com. blah... Blah... blah... http://my_website_url.com/user/ Blah... blah... blah...http://external_links.com Blah... blah... blah... Blah... blah... blah...";

    $words = explode(' ', $comment);
    foreach($words as &$word){
        if(strpos($word, 'my_website_url.com') == true){
            $word = '<a href="http://'.$word.'">'.$word.'</a>';
        }
    }

    echo implode(' ', $words);

?>

Output:

Blah... blah... blah... <a href="http://http://my_website_url.com,">http://my_website_url.com,</a> Blah... blah... <a href="http://http://my_website_url.com<br><br>Blah...">http://my_website_url.com<br><br>Blah...</a> <a href="http://blah...http://my_website_url.com.">blah...http://my_website_url.com.</a> blah... Blah... blah... <a href="http://http://my_website_url.com/user/">http://my_website_url.com/user/</a> Blah... blah... blah...http://external_links.com Blah... blah... blah... Blah... blah... blah...

As you can see the output, the "," or "." or "<br><br>" are included into the links. Also, the "http://" will double if the URL contain "http://". Can anyone help me to edit the code?

Many thanks....

Thanks guys, I tested all above examples with many ways, but not really working to me and i want it to insert into database with whole comment after replace the links. I have the code below, it will replace my URLs to links. But, the problems are same. Please see the output below after the code:

<?
$comment = "Blah... blah... blah... http://my_website_url.com, Blah... blah... http://my_website_url.com<br><br>Blah... blah...http://my_website_url.com. blah... Blah... blah... http://my_website_url.com/user/ Blah... blah... blah...http://external_links.com Blah... blah... blah... Blah... blah... blah...";

    $words = explode(' ', $comment);
    foreach($words as &$word){
        if(strpos($word, 'my_website_url.com') == true){
            $word = '<a href="http://'.$word.'">'.$word.'</a>';
        }
    }

    echo implode(' ', $words);

?>

Output:

Blah... blah... blah... <a href="http://http://my_website_url.com,">http://my_website_url.com,</a> Blah... blah... <a href="http://http://my_website_url.com<br><br>Blah...">http://my_website_url.com<br><br>Blah...</a> <a href="http://blah...http://my_website_url.com.">blah...http://my_website_url.com.</a> blah... Blah... blah... <a href="http://http://my_website_url.com/user/">http://my_website_url.com/user/</a> Blah... blah... blah...http://external_links.com Blah... blah... blah... Blah... blah... blah...

As you can see the output, the "," or "." or "<br><br>" are included into the links. Also, the "http://" will double if the URL contain "http://". Can anyone help me to edit the code?

Many thanks....

Did you try adding in my bit?

<?
$comment = "Blah... blah... blah... http://my_website_url.com, Blah... blah... http://my_website_url.com<br><br>Blah... blah...http://my_website_url.com. blah... Blah... blah... http://my_website_url.com/user/ Blah... blah... blah...http://external_links.com Blah... blah... blah... Blah... blah... blah...";

    $chars = array(' ','<br>','<br/>',',','!','http://');
    $words = explode($chars, $comment);
    foreach($words as &$word){
        if(strpos($word, 'my_website_url.com') == true){
            $lastchardelete = array('?','.');
            if(in_array(substr($word,-1),$lastchardelete)){
                $word = substr($word,0,-1);
            }
            $word = '<a href="http://'.$word.'">'.$word.'</a>';
        }
    }

    echo implode(' ', $words);

?>

Nope, it's not working. whole comment become a link:

<a href="http://Blah... blah... blah... http://my_website_url.com, Blah... blah... http://my_website_url.com<br><br>Blah... blah...http://my_website_url.com. blah... Blah... blah... http://my_website_url.com/user/ Blah... blah... blah...http://external_links.com Blah... blah... blah... Blah... blah... blah..">Blah... blah... blah... http://my_website_url.com, Blah... blah... http://my_website_url.com<br><br>Blah... blah...http://my_website_url.com. blah... Blah... blah... http://my_website_url.com/user/ Blah... blah... blah...http://external_links.com Blah... blah... blah... Blah... blah... blah..</a>

I remove:

$chars = array(' ','<br>','<br/>',',','!','http://');

and change back to

$words = explode('', $comment);

then it works like before but still having the problem of commas and line break things. I think it's the multiple delimiters to cause the script not working.

I also tried to change this line from question mark and full stop to comma and line break:

$lastchardelete = array(',','<br>');

It delete the comma after the URL and link, but the line break still there.

I found another problem is, the word before the URL cannot be explode.:

blah...http://my_website_url.com

Any help?

Sorry it looks like explode won't accept an array, the alternative is using preg_split() but i've never been good with reg exp.

$words = preg_split("/[,\. ]/", $comment);

No idea how to add <br> into that, another alternative i see is just use str_replace first

$chars = array(' ','<br>','<br/>',',','!','http://');
$step1 = str_replace($chars, '+delim+', $comment); //Extra step to create a uniform value
$words = explode('+delim+', $step1);

http://stackoverflow.com/questions/2860238/exploding-by-array-of-delimiters

Also add this for line breaks

$lastchardelete = array(',','<br>',"\r\n","\r","\n");

I'm hoping it replaces in order so that won't leave the odd \r on the end

How about this? It does not included query parameters (yet):

$new_comment = preg_replace('%(http://)?(www\.)?(my_website_url\.com)([/\w]*)?%m', '<a href="\1\2\3\4">\1\2\3\4</a>', $comment);

Note that it may need some tweaking to get all your url's correctly.

commented: like it +14

Thank you guys, problem solved. Thanks pritaeas, it works! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.