【问题标题】:PHP - Replace Word in String (while ignore HTML tags)PHP - 替换字符串中的单词(同时忽略 HTML 标签)
【发布时间】:2016-01-26 08:06:26
【问题描述】:

我有一个带有 HTML 标签的字符串,$paragraph:

$paragraph = '
    <p class="instruction">
        <sup id="num1" class="s">80</sup>
        Hello there and welcome to Stackoverflow! You are welcome indeed.
    </p>
';

$replaceIndex = array(0, 4);
$word = 'dingo';

我想替换由$replaceIndex(0 和4)定义的索引处的单词$paragraph。我的意思是我想用$word 替换“80”和“欢迎”(仅是第一个实例)这两个词。段落本身可能在不同位置使用不同的 HTML 标记进行格式化。

有没有办法在几乎忽略(但不剥离)HTML 标记的情况下定位和替换字符串中的某些单词?

谢谢!

编辑:单词由(多个)标签和(多个)空白字符分隔,而不包括标签内的任何内容。

【问题讨论】:

  • 是什么让 80 位居第 0 位并在第 4 位受到欢迎?
  • 这些(“80”和“welcome”)是否多次出现?如果不是这样,str_replace 应该很容易
  • 你能告诉我们这里使用什么标准来选择索引 0 和 4 处的单词,是不是必须在前面有一个空格,然后是一个空格或类似的东西。跨度>
  • @urban:是的,单词可能出现多次。
  • @Hanky웃Panky:单词由(多个)标签或(多个)空白字符分隔,标签内不包含任何内容。 *我认为它们被称为空白字符……空格、换行符、制表符等)

标签: php html regex


【解决方案1】:

感谢所有提示。我想到了!由于我是 PHP 新手,如果任何 PHP 资深人士对简化代码有任何提示,我将不胜感激。谢谢!

$paragraph = '
    <p class="instruction">
        <sup id="num1" class="s">80</sup>
        Hello there and welcome to Stackoverflow! You are welcome indeed.
    </p>
';

// Split up $paragraph into an array of tags and words
$paragraphArray = preg_split('/(<.*?>)|\s/', $paragraph, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$wordIndicies = array(0, 4);
$replaceWith = 'REPLACED';

foreach ($wordIndicies as $wordIndex) {
    for ($i = 0; $i <= $wordIndex; $i++) {
        // if this element starts with '<', element is a tag.
        if ($paragraphArray[$i]{0} == '<') {
            // push wordIndex forward to compensate for found tag element
            $wordIndex++;
        }
        // when we reach the word we want, replace it!
        elseif ($i == $wordIndex) {
            $paragraphArray[$i] = $replaceWith;
        }
    }
}

// Put the string back together
$newParagraph = implode(' ', $paragraphArray);

// Test output!
echo(htmlspecialchars($newParagraph));

*唯一需要注意的是,这可能会在 $newParagraph 中产生不需要的空格,但我会在实现代码时看看这是否真的会导致任何问题。

【讨论】:

    【解决方案2】:
    $text = preg_replace('/\b80\b|\bwelcome\b/', $word, $paragraph);
    

    希望对你有帮助:)

    【讨论】:

    • 一位评论者帮助我澄清:单词可能出现多次,我只是试图替换它的某个实例。我有兴趣替换指定索引处的单词,而不是预定义的单词。不过谢谢你的回答!
    • 为此,您需要在要替换的字符串中定义 $NUMBER 和 $WORD。正则表达式可以做到。
    【解决方案3】:

    SimpleXML 也可以派上用场:

    $paragraph = '
        <p class="instruction">
            <sup id="num1" class="s">80</sup>
            Hello there and welcome to Stackoverflow! You are welcome indeed.
        </p>
    ';
    
    $xml = simplexml_load_string($paragraph);
    $xml->sup = $word;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 2014-08-16
      • 1970-01-01
      相关资源
      最近更新 更多