如何使用 preg_replace PHP 删除文本中的域名 [英] How to delete domain name in Text using preg_replace PHP

查看:51
本文介绍了如何使用 preg_replace PHP 删除文本中的域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的示例文本:

Nytimes.com 最大的新闻门户之一,里面有很多内容.www.Google.com、www.yahoo.com 和 yandex.ru 等几个搜索引擎将它们的爬虫发送到这些站点.这也 Bing.com 删除了

Nytimes.com One of the biggest news portals, have a lot of contents inside. Several search engine like www.Google.com, www.yahoo.com and yandex.ru send their crawler to this sites. This also Bing.comremoved

所以我想使用正则表达式 preg_replace 删除里面的所有域.结果应该是:

So I want use regex preg_replace to remove all domain inside. The results should be:

最大的新闻门户之一,里面有很多内容.几个搜索引擎喜欢并将他们的爬虫发送到这个网站.这也删除了

One of the biggest news portals, have a lot of contents inside. Several search engine like and send their crawler to this sites. This also removed

这是我失败的实验:

$new_text = preg_replace('/s(.*?)\.(?:aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|cz|de|dj|dk|dm|do|dz|ec|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mn|mn|mo|mp|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|nom|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ra|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw|arpa)/', '', $text);

有什么办法可以解决这个问题吗?

Any solution to solve this problem?

推荐答案

这样的事情应该对您有所帮助:

Something like this should help you:

/([wW]{3,3}\.|)[A-Za-z0-9]+?\.(se|com|ru)/

用你需要的代替最后一个 se|com|ru.虽然在这之后你会有这样的事情:

replace the last se|com|ru with what you need instead. Although after this you'll have something like this:

最大的新闻门户之一,里面有很多内容.几个搜索引擎喜欢 ,并将他们的爬虫发送到这个网站.这也删除了

One of the biggest news portals, have a lot of contents inside. Several search engine like , and send their crawler to this sites. This also removed

要删除开头的空格,您可以执行 trim($str) 和其他空格的正则表达式,看起来像 preg_replace('/\s+/', ' ', $str); 应该可以帮助你.

To remove the space in the start you could do trim($str) and for the other spaces a regex that looks something like preg_replace('/\s+/', ' ', $str); should help you.

这篇关于如何使用 preg_replace PHP 删除文本中的域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆