使用PHP从给定的HTML代码中获取内部HTML


get the inner html using php from the given html code

这是一个非常基本的问题,但是找不到答案

我想获得内部html使用php从以下html代码

<strong class="large t55">text only</strong>

我想在简单文本中显示"text only"而不是粗体

http://php.net/manual/en/function.strip-tags.php

echo strip_tags('<strong class="large t55">text only</strong>');

如果你只想获得内部文本,strip_tags最快最简单的解决方案。

但是对于一般的解析HTML(例如获取属性:名称,类,…)最好的解决方案是DOMDocument.

试试HTML DOM

<?php 
  $html= "<strong class='large t55'>text only</strong>";
  $dom = new domDocument('1.0', 'utf-8'); 
  $dom->loadHTML($html); 
  $dom->preserveWhiteSpace = false; 
  $hTwo= $dom->getElementsByTagName('strong'); 
  echo $hTwo->item(0)->nodeValue; 
 ?>