为每件事做点什么

时间:2016-04-26 14:49:23

标签: javascript php jquery

我在索引页面上有div,它是可编辑的。 在div下面我有按钮发布。 点击按钮我将div内容发送到另一个名为file.php的页面 在这个文件里面我有

$text = $_POST['text'];

现在我需要以某种方式检查我在$text

中有多少开合跨度

示例:

$text = "<span id='first'>first</span> some text <span id='second'>second</span> more text <span id='third'>third</span>";

现在foreach开启和关闭跨度foreach跨度做其他事情...... 我如何检查我在字符串中有多少跨度,并在其他地方做其他事情......

更新: 我试过这个:

$html = str_get_html($text);
foreach($html->find('a') as $element){ 
       echo $element->href . '<br>';
}

我在str_get_html中找到了未定义函数的致命错误

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$text = "<span id='first'>first</span> some text <span id='second'>second</span> more text <span id='third'>third</span>";

$dom = new DOMDocument;
$dom->loadHTML($text);
foreach( $dom->getElementsByTagName('span') as $node)
{
    echo $node->nodeValue . "<br/>"; 
}

在这里查看:PHP DOM: parsing a HTML list into an array?

相关问题