1

p我有一个很长的 HTML 文本,我想在其中迭代PHP 中标签的id 值。我的原始字符串:

$mystring="
<p> my very long text with a lot of words ....</p>
<p></p>
<p> my other paragraph with a very long text ...</p>
(...)
";

我想要的结果:

$myparsestring= "
<p id=1>my very long text with a lot of words ....</p>
<p id=2> my other paragraph with a very long text ...</p>
";

如您所见,我可以使用getElementsByTagName ()和正则表达式(可能会拆分)。

您对这项工作有何指导?

4

2 回答 2

3

如果您打算解析 html,请尝试将DOMxpath一起使用。

这是一个简单的例子:

$xpath = new DOMXPath($html);
$query = '//*/p';
$entries = $xpath->query($query);

不要使用正则表达式,如果您打算像这样解析 html,请使用此方法,除非您有使用正则表达式的特定原因

于 2012-11-14T11:15:39.957 回答
0

你可以regex这样去:

$mystring="
<p> my very long text with a lot of words ....</p>
<p></p>
<p> my other paragraph with a very long text ...</p>
(...)
";

// This will give you all <p> tags, that have some information in it.
preg_match_all('/<p>(?<=^|>)[^><]+?(?=<|$)<\/p>/s', $mystring, $matches);

$myparsestring = '';
for( $k=0; $k<sizeof( $matches[0] ); $k++ )
{
    $myparsestring .= str_replace( '<p', '<p id='.($k+1), $matches[0][$k] );
}

echo htmlspecialchars( $myparsestring );

和输出/结果:

<p id=1> my very long text with a lot of words ....</p>
<p id=2> my other paragraph with a very long text ...</p>
于 2012-11-14T11:13:30.117 回答