简体   繁体   中英

PHP Remove a string that starts with http:// or www and ends with .com/.ca/.net etc

I found a bunch of link that show how to delete anything within with str_replace() but I want to censor the link on a post. Even better, somehow detect when the user is trying to post a link and not let them submit the form until they remove it

I am not to sure what to write inside str_replace() or how to check the page for inserted urls

Any help is appreciated!

您需要使用preg_match()检查正则表达式的提交字符串。

preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $string)

This could be achieved with regular expression. A kind of pattern comparision

Like this:

$pattern = "/(?i)(<a([^>]+)>(.+?)<\/a>)/";
$output = preg_replace ( $pattern , "Censured link",$inputText);

//assuming $inputText contains your input

this will replace all anchors with the text Censured link

you can use these functions(little bit ease)

$link1="http://www.asda.com";
$link2="http://www.asda.net";
$link3="http://www.asda.ca";
function startsWith($to, $find)
{
    return $find=== "" || strpos($to, $find) === 0;
}
function endsWith($to, $find)
{
    return $find=== "" || substr($to, -strlen($find)) === $find;
}

var_dump(startsWith($link1, "https")); // true
var_dump(endsWith($link2, "au")); 
...so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM