[英]PHP - remove words (http|https|www|.com|.net) from string that do not start with specific words
I have a string with some text and some URLs in it. 我有一个带有一些文本和一些URL的字符串。 My goal is to remove the following from the string:
我的目标是从字符串中删除以下内容:
$removeThis = array('http://', 'https://', 'www.', '.com', '.net'); $ removeThis = array('http://','https://','www。','.com','.net');
BUT ONLY IF the word to be removed doesn't start with: http://good.com , http://www.good.com , https://good.com , or https://www.good.com . 但是仅当要删除的单词不是以以下开头时: http : //good.com,http : //www.good.com,https : //good.com或https://www.good.com 。
In other words, I want to remove http|s|www.|.com|.net parts from the string (but only if they don't belong to good.com domain). 换句话说,我想从字符串中删除http | s | www。| .com | .net部分(但仅当它们不属于good.com域时)。
INPUT: 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';
RESULT SHOULD BE: 结果应为:
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
I think preg_replace is needed here.. 我认为在这里需要preg_replace。
try below: 请尝试以下方法:
$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);
This might help you: 这可能对您有帮助:
$url = "www.good.net/tooooo.php";
$regex = array('/(https?:..)/','/^www\./','/(\.com.|\.net.|\.co.)+([^\s]+)/');
$url = preg_replace($regex, '', $url);
echo $url;
You should use REGEX which are really powerful, here the step to do it pretty easily : 您应该使用真正强大的REGEX,在这里轻松完成此步骤:
Regex for urls : 网址的正则表达式:
#^(https?|ftp):\/\/(-\.)?([^\s\/?\.#]+\.?)+(\/[^\s]*)?$#
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.