PHP的SimpleXML找到HTML标签的文本XML元素内
问题描述:
如何在XML PHP变量设置为一切<STRONG>
标签中所示:PHP的SimpleXML找到HTML标签的文本XML元素内
<SAVED_EXPORT>
<metatag_description>
Some text here? The <strong> Cambro High-Impact 12" x 16" Dietary Tray</strong> is the solution. Comprised of high-impact, ligh
</metatag_description>
</SAVED_EXPORT>
<? foreach($xml as $SAVED_EXPORT) {
$header = $SAVED_EXPORT->metadescription;
echo $header[0];
}
?>
我希望它只是吐出:寒武高影响12" ×16" 膳食托盘,而不是所有的描述
答
尝试增加,而不是你的回音下面的表达式:
if (preg_match("/\<\;strong\>\;(.*?)\<\;\/strong\>\;/si", $header[0], $match) == true)
{
echo $match[1];
}
答
使用strpos和substr:
$string = 'Some text here? The <strong> Cambro High-Impact 12" x 16" Dietary Tray</strong> is the solution. Comprised of high-impact, ligh';
// Tag to look for
$tag = 'strong';
$start_tag = '<' . $tag . '>';
$end_tag = '</' . $tag . '>';
// 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));
此示例更加动态,因为它遍历字符串以查找起始位置。但是,它不那么动态,因为它假定程序员知道字符串的长度。 – 2012-06-25 21:00:07
@nodirtyrockstar - 那么,现在多一点动态。感谢您的输入! – fourreux 2012-07-24 19:25:24