我有一个字符串,里面有一些文本和一些URL。我的目标是从字符串中删除以下内容:
$removeThis =数组(‘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域)。
投入:
$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 ..。
发布于 2015-04-27 02:30:37
试着在下面:
$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);发布于 2015-04-27 02:01:50
这可能对你有帮助:
$url = "www.good.net/tooooo.php";
$regex = array('/(https?:..)/','/^www\./','/(\.com.|\.net.|\.co.)+([^\s]+)/');
$url = preg_replace($regex, '', $url);
echo $url;发布于 2015-04-27 02:21:00
您应该使用非常强大的REGEX,这里的步骤非常简单:
urls的Regex:
#^(https?|ftp):\/\/(-\.)?([^\s\/?\.#]+\.?)+(\/[^\s]*)?$#https://stackoverflow.com/questions/29885882
复制相似问题