在Woocommerce的所有导入中按产品描述删除外部链接

时间:2019-02-18 19:12:37

标签: php regex wordpress woocommerce wpallimport

我必须从产品说明中删除外部URL,这是一个示例:

用于摄像机佳能NB-5L的Powerbank:https://www.esseshop.it/caricabatterie-universale-da-auto-rete-fotocamera-videocamera-p-4452.html

所以我必须使用正则表达式删除以http开头,以.html或.htm结尾的所有子字符串

$str = "Powerbank for videocamera Canon NB-5L: https://www.esseshop.it/caricabatterie-universale-da-auto-rete-fotocamera-videocamera-p-4452.html";

preg_replace('(http)|(.html)|(.htm)', '$1', $str, 1);

2 个答案:

答案 0 :(得分:1)

您可以使用此正则表达式将其与以http:https:开头的任何URL匹配

https?:\S*

Demo

PHP代码演示

$str = "Powerbank for videocamera Canon NB-5L: https://www.esseshop.it/caricabatterie-universale-da-auto-rete-fotocamera-videocamera-p-4452.html";
echo preg_replace('/https?:\S*/', '', $str, 1);

打印

Powerbank for videocamera Canon NB-5L:

答案 1 :(得分:0)

您的模式(http)|(.html)|(.htm)使用带有3个捕获组的替换,并且在代码中使用组1作为替换。请注意对点进行转义以使其在字面上匹配。

如果该网址应以htm或html结尾,则可以使用:

\bhttps?:\S+\.html?\b

说明

  • \bhttps?:单词边界\b,以防止http成为更长匹配单词的一部分
  • \S+匹配1次以上而不是空格字符
  • \.html?\b匹配一个点,后跟htm和一个可选的l。最后一个单词边界可以防止html?成为更长匹配词的一部分

Regex demo | php demo