繁体   English   中英

PHP DOMDocument用文本节点替换多个子节点

[英]PHP DOMDocument replace multiple child with text nodes

我有一个示例字符串如下:

$feed_status = "Nice to see you all back again <img src=\"http://52.1.47.143/file/pic/emoticon/default/smile.png\" alt=\"Smile\" title=\"Smile\" title=\"v_middle\" /><img src=\"http://52.1.47.143/file/pic/emoticon/default/smile.png\" alt=\"Smile\" title=\"Smile\" title=\"v_middle\" /><img src=\"http://52.1.47.143/file/pic/emoticon/default/smile.png\" alt=\"Smile\" title=\"Smile\" title=\"v_middle\" />";

(请原谅我的语法错误,例如放置单引号和双引号)

为了您的参考,我刚刚在字符串中添加了三个<img>标签,但在实际情况下,此字符串可能包含一个<img>标签或没有<img>标签或多个<img>标签。

我想获取每个<img>标签的src属性中存在的文件名,并创建这些文件名的数组。 然后我必须用名为$emoticon_codes的数组中的字符串替换这些<img>标签,该数组是根据<img>标签中的文件名动态创建的。 这种字符串的替换应该是相同的顺序。

为此,我尝试了以下代码。 直到创建名为$emoticon_codes的动态数组一切正常,但我面临的代码是用数组$emoticon_codes的字符串替换当前的<img>标签。 那么有人可以帮我纠正我在代码中犯的错误,同时从字符串中替换<img>标签。

以下是我的代码:

  $doc = new DOMDocument();
  $doc->loadHTML($feed_status);
  $imageTags = $doc->getElementsByTagName('img');

  if(count($imageTags)) {
    $emoticon_codes = array();
    foreach($imageTags as $tag) {
      if (basename($tag->getAttribute('src')) == 'evilgrin.png') {
        array_push($emoticon_codes, '\ue404');
      }
      if (basename($tag->getAttribute('src')) == 'grin.png') {
        array_push($emoticon_codes, '\ue415');
      }
      if (basename($tag->getAttribute('src')) == 'happy.png') {
        array_push($emoticon_codes, '\ue057');
      }
      if (basename($tag->getAttribute('src')) == 'smile.png') {
        array_push($emoticon_codes, '\ue056');
      }
      if (basename($tag->getAttribute('src')) == 'surprised.png') {
        array_push($emoticon_codes, '\ue107');
      }
      if (basename($tag->getAttribute('src')) == 'tongue.png') {
        array_push($emoticon_codes, '\ue105');
      }
      if (basename($tag->getAttribute('src')) == 'unhappy.png') {
        array_push($emoticon_codes, '\ue403');
      }
      if (basename($tag->getAttribute('src')) == 'waii.png') {
        array_push($emoticon_codes, '\ue407');
      }
      if (basename($tag->getAttribute('src')) == 'wink.png') {
        array_push($emoticon_codes, '\ue405');
      }
    }
    /*Till here everything works fine. The array $emoticon_codes is also getting generated finely*/

    /*Following is the code giving problem to me,*/
    $t = 0;

    foreach($imageTags as $img) {
      $img->parentNode->replaceChild($img, $doc->createTextNode($emoticon_codes[$t]));
      $t++;
      if ($t > count($emoticon_codes)) {
        break;
      }
    }
  }

echo $feed_status;之后,我想要的输出字符串应如下所示echo $feed_status;

$ feed_status =很高兴再次见到你\\ ue056 \\ ue056 \\ ue056;

提前致谢。

如果您正在尝试更改多个子项,则需要进行一些回归才能进行一些更改,我建议尝试在数组中映射每个替换,而不是使用多个if语句。 例:

$feed_status = "Nice to see you all back again <img src=\"http://52.1.47.143/file/pic/emoticon/default/smile.png\" alt=\"Smile\" title=\"Smile\" title=\"v_middle\" /><img src=\"http://52.1.47.143/file/pic/emoticon/default/smile.png\" alt=\"Smile\" title=\"Smile\" title=\"v_middle\" /><img src=\"http://52.1.47.143/file/pic/emoticon/default/smile.png\" alt=\"Smile\" title=\"Smile\" title=\"v_middle\" />";

$doc = new DOMDocument();
@$doc->loadHTML($feed_status, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$imageTags = $doc->getElementsByTagName('img');

$replacements = array(
    'evilgrin.png' => '\ue404',
    'grin.png' => '\ue415',
    'happy.png' => '\ue057',
    'smile.png' => '\ue056',
    'surprised.png' => '\ue107',
    'tongue.png' => '\ue105',
    'unhappy.png' => '\ue403',
    'waii.png' => '\ue407',
    'wink.png' => '\ue405',
);

// regression 
$i = $imageTags->length - 1;
while($i > -1) {
    $tag = $imageTags->item($i);
    $basename = basename($tag->getAttribute('src'));
    if(isset($replacements[$basename])) { // if the file name matches
        // make replacements
        $r = $replacements[$basename];
        $text = $doc->createTextNode($r);
        $tag->parentNode->replaceChild($text, $tag);
    }
    $i--;
}
// append to string container again
$feed_status = '';
foreach($doc->childNodes->item(0)->childNodes as $e) {
    $feed_status .= $doc->saveHTML($e);
}
echo $feed_status;

样本输出

你有这个:

foreach($imageTags as $img) {
  $img->parentNode->replaceChild($img, $doc->createTextNode($emoticon_codes[$t]));
  $t++;
  if ($t > count($emoticon_codes)) {
    break;
  }
}

但是只能通过$imageTags循环,而不是$emoticon_codes

你需要这个:

foreach($imageTags as $img) {
      foreach($emoticon_codes as $emoticon_code) {
              $img->parentNode->replaceChild($img, $doc->createTextNode($emoticon_code));
      }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM