正则表达式清除一个 WORD HTML 标签

Regex To clear out a WORD HTML Tag

我有一些从 word 中解析出来的文本,我正在尝试删除 HYPERLINK 部分。我试过以下方法,我做错了什么?

preg_replace("/(HYPERLINK "\"{2})/", "", $input_string);

这是我希望发生的事情的示例。

words words words HYPERLINK "https://whosebug.com" https://whosebug.com words words words 

应该变成

words words words https://whosebug.com words words words 

只需使用以下模式并替换为空字符串:

/HYPERLINK +"[^"]+" */

DEMO

PHP

preg_replace('/HYPERLINK +"[^"]+" */', "", $input_string);

解释

  NODE                     EXPLANATION
--------------------------------------------------------------------------------
  HYPERLINK                'HYPERLINK'
--------------------------------------------------------------------------------
   +                       ' ' (1 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
  [^"]+                    any character except: '"' (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
   *                       ' ' (0 or more times (matching the most
                           amount possible))

另请检查 The Stack Overflow Regular Expressions FAQ 以阅读有关正则表达式的更多信息

试试这样的:

preg_replace('/HYPERLINK "(.*?)"/', "", $input_string);

preg 替换

echo preg_replace('/HYPERLINK +"[^"]+" */', "", $input_string); // should do it

learn regular expression

说明

  • 字面上匹配字符
  • 量词:+ 在一次和无限次之间,尽可能多次,按需回馈[贪心]
  • " 按字面意思匹配字符 "
  • [^"]+ 匹配下面列表中不存在的单个字符
  • 量词:+ 在一次和无限次之间,尽可能多次,按需回馈[贪心]
  • “列表中的单个字符”字面意思(区分大小写)
  • " 按字面意思匹配字符 "

旧解

$input_string = 'words words words HYPERLINK "https://whosebug.com" https://whosebug.com words words words';  

$words = explode(' ', $input_string);
foreach (array_keys($words, 'HYPERLINK') as $key) {
  unset($words[$key+1]);
}
$sentence = implode(' ', $words);

echo $sentence = str_replace('HYPERLINK ', '', $sentence);