I have a problem, want to make script which will cutting off from the page unwanted links, which are posted by users of my website. I now how to cut off name of this site, but have no idea how to get rid off "http://www." and everything after "/" slash in the address. Here's my code: $text = preg_replace("'(example.com)'i", '', $text); PHP: I'll be very grateful for help
<?php $wrongurl="www.hotmail.com/asdasd/asdasd/adasd"; $array=array("www.","http://"); $text2=str_ireplace($array,"",$wrongurl); $str = explode('/', $text2); $text= $str[0]; echo $text; ?> PHP: Try this, it will replace "http://", "www." and all letters after "/" in the url. For example in above code: 1. "http://www.hotmail.com/asdasd/asdasd/adasd" will only show you "hotmail.com" 2. "http://hotmail.com/asdasd/asdasd/adasd" will also show you only "hotmail.com" 3. "www.hotmail.com/asdasd/asdasd/adasd" will also show you only "hotmail.com" Thanks, hope it helps
That's doesn't work neither, and bear in mind that I want to delete whole url http://www.example.com/something-after-slash HTML: and replace with message [link prohibited]
Try this: <?php $wrongurl="www.hotmail.com/"; $array=array("www.","http://"); $text2=str_ireplace($array,"",$wrongurl); $str = explode('/', $text2); if ($str[1]){ echo "[Link Prohibited!]"; }else{ echo "http://www.".$str[0]; } ?> PHP: if entered url have any text after slash, it will replace it with "Link Prohibited".
This doesn't work I'm getting http://www.hotmail.com HTML: at the top of every page. I give You broader look on the code which I have now: $text = ' ' . $text . ' '; $text = preg_replace("'example.com'i", '', $text); return $text; HTML: This code works, I mean it cutting off links example.com HTML: BUT leaves http://www. HTML: and /something-after-slash HTML:
Use this: $text = ' ' . $text . ' '; $text="example.com"; $array=array("www.","http://"); $text2=str_ireplace($array,"",$text); $str = explode('/', $text2); if ($str[1]){ $text= "[Link Prohibited!]"; }else{ $text= "http://www.".$str[0]; } return $text; PHP:
Use regular expression $string = "Bla bla bla..Visit http://regex.kzzen.com for more info. <br /> Visit http://kzzen.com/2008/10/if-you-want-to-earn-money-online/ for more info.<br /> Visit http://www.kzzen.com/?act=what for more info.<br /> Visit www.kzzen.com/?act=what for more info.<br /> "; $pattern = "/http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; $string = preg_replace($pattern,'[Link Prohibited!]',$string); print $string; PHP: This output : Note that all links start with http:// were replaced. Without http:// it won't be replaced. And it stop replacing when it found a whitespace. You can use my Regex Tester and find a better solution, maybe.
ads2help Your last solution works just like You said "all links with http:// are replaced with [Link prohibited] message", so it's all right then, but in my case I want to replace only links to a one particular website, is this possible?
$rejected_website = 'http:\/\/www.example.com'; $string = "Bla bla bla..Visit http://regex.kzzen.com for more info. <br /> Visit http://www.example.com/something-after-slash for more info.<br /> Visit http://www.example.com for more info.<br /> Visit www.kzzen.com/?act=what for more info.<br /> "; $pattern = "/$rejected_website(\/\S*)?/"; $string = preg_replace($pattern,'[Link Prohibited!]',$string); print $string; PHP: Output :
All right its almost working, link http://www.example.com/something HTML: has been cutt off, and replaced with [link prohibitted] msg, but there are still links: http://example.com/something HTML: www.example.com/something HTML: and example.com/something HTML: -- edit: I made up something like that: $rejected_website = 'example.com'; $pattern = "/(http:\/\/www)?$rejected_website(\/\S*)?/"; PHP: but I'm getting www.[Link prohibited] HTML: http://[Link progibited] HTML: or http://www.[Link prohibited] HTML:
$rejected_website = 'example.com'; $string = "Bla bla bla..Visit http://regex.kzzen.com for more info. <br /> Visit http://www.example.com/something-after-slash for more info.<br /> Visit http://www.example.com for more info.<br /> Visit example.com for more info.<br /> Visit http://example.com for more info.<br /> Visit notrejected.com for more info.<br /> Visit www.notrejected.com for more info.<br /> "; $pattern = "/(http:\/\/)?(www.)*$rejected_website(\/\S*)?/"; $string = preg_replace($pattern,'[Link Prohibited!]',$string); print $string; PHP: output : is this ok?
Hi ads2help That's works! I'm so much grateful to You! I warn You, that if I'd have some problems with reg. expressions, I'll call You :-D Cheers