简体   繁体   中英

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. My goal is to remove the following from the string:

$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 .

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).


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..

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 :

  1. Match all urls using preg_replace_callback
  2. In callback function, detect if it belongs to the whitelisted domain or not (preg_match or strrpos)
  3. Still in callback function : Treat the string in consequence and return it

Regex for urls :

#^(https?|ftp):\/\/(-\.)?([^\s\/?\.#]+\.?)+(\/[^\s]*)?$#

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