我有一个包含一些文字和一些网址的字符串。我的目标是从字符串中删除以下内容:
$ removeThis = array('http://','https://','www。','。com','。net');
但仅 要删除的字词无法启动:http://good.com,http://www.good.com, https://good.com或https://www.good.com。
换句话说,我想从字符串中删除http | s | www。| .com | .net部分(但前提是它们不属于good.com域)。
INPUT:
$string='Hello world, this is spamming: www.spam.com, spam.net, https://spam.com, https://spam.com/tester. And this is not spam so do not touch it: http://www.good.com/okay, http://good.com, and also https://good.com/well';
结果应该是:
Hello world, this is spamming: spam, spam, spam, spam/tester. And this is not spam so do not touch it: http://www.good.com/okay, http://good.com, and also https://good.com/well
我认为这里需要preg_replace ..
答案 0 :(得分:1)
尝试以下:
$preg = '/(?:(http|https):\/\/)?(?:www\.)?\w+\.(com|net)/i';
$str = preg_replace_callback($preg, function($matches) {
$removeThis = array('/http:\/\//i', 'https://', 'www.', '.com', '.net');
if (preg_match('/(http|https):\/\/(www\.)?good\.(com|net)/i', $matches[0])) return $matches[0];
return preg_replace('/((http|https):\/\/|www\.|\.com|\.net)/i', '', $matches[0]);
}, $string);
答案 1 :(得分:0)
这可能会对您有所帮助:
$url = "www.good.net/tooooo.php";
$regex = array('/(https?:..)/','/^www\./','/(\.com.|\.net.|\.co.)+([^\s]+)/');
$url = preg_replace($regex, '', $url);
echo $url;
答案 2 :(得分:0)
你应该使用非常强大的REGEX,这里很容易做到这一步:
网址的正则表达式:
#^(https?|ftp):\/\/(-\.)?([^\s\/?\.#]+\.?)+(\/[^\s]*)?$#