php simplexml在xml元素中查找html标签的文本

时间:2012-03-07 17:34:42

标签: php xml xpath simplexml

如何将php变量设置为xml中以下<STRONG>标记内的所有内容:

<SAVED_EXPORT>
    <metatag_description>
    Some text here? The &lt;strong&gt; Cambro High-Impact 12" x 16" Dietary Tray&lt;/strong&gt; is the solution. Comprised of high-impact, ligh
    </metatag_description>
</SAVED_EXPORT>

<?    foreach( $xml as $SAVED_EXPORT ) {

        $header = $SAVED_EXPORT->metadescription;
        echo $header[0];
        } 
?>

我希望它能吐出来: Cambro High-Impact 12“x 16”膳食托盘而非所有描述

2 个答案:

答案 0 :(得分:0)

尝试添加以下表达式而不是echo:

if (preg_match("/\&lt\;strong\&gt\;(.*?)\&lt\;\/strong\&gt\;/si", $header[0], $match) == true)
{
    echo $match[1];
}

答案 1 :(得分:0)

使用strpos和substr:

$string = 'Some text here? The &lt;strong&gt; Cambro High-Impact 12" x 16" Dietary     Tray&lt;/strong&gt; is the solution. Comprised of high-impact, ligh'; 

// Tag to look for
$tag       = 'strong';
$start_tag = '&lt;' . $tag . '&gt;';
$end_tag   = '&lt;/' . $tag . '&gt;';

// Determine position of search pattern <strong>
$begin = strpos($string, $start_tag);
// Add the lenght of the tag string:
$begin += strlen($start_tag);
// Determine position of search pattern </strong>
$end   = strpos($string, $end_tag);
// Calculate length:
$length = $end - $begin;

// echo the trimmed substring
echo trim(substr($string, $begin, $length));