按 Woocommerce 的 All-Import 中的产品描述删除外部 link

Removing external link by product description in All-Import for Woocommerce

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

佳能 NB-5L 摄像机移动电源: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)', '', $str, 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:

您的模式 (http)|(.html)|(.htm) 使用 3 个捕获组的交替,在代码中使用组 1 作为替换。注意转义点以按字面匹配它。

如果 url 应该以 htm 或 html 结尾,您可以使用:

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

说明

  • \bhttps?: 单词边界 \b 以防止 http 成为较长匹配单词的一部分
  • \S+匹配1+次不是空白字符
  • \.html?\b 匹配一个点后跟 htm 和一个可选的 l。末尾一个词的分界是为了防止html?作为较长匹配词的一部分

Regex demo | php demo