删除以http://或www开头并以.com/.ca/.net等结尾的字符串


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

我发现了一堆链接,显示如何删除与str_replace()内的任何东西,但我想审查一个帖子上的链接。更好的是,当用户试图发布链接时,以某种方式检测并不让他们提交表单,直到他们删除它

我不确定在str_replace()中写什么或如何检查页面中插入的url

任何帮助都是感激的!

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

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

这可以通过正则表达式实现。一种模式比较

:

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

这将用文本cenured link

替换所有锚

你可以使用这些函数(稍微轻松一点)

$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