PHP替换<;h1></h1>;标签


PHP replace text within a <h1> </h1> tag

我正在使用AJAX调用一个PHP文件,该文件将有效地编辑另一个HTML文件中的特定内容。我的问题是,我不确定针对这些特定领域的最佳方法。

我认为需要在需要编辑的标签或注释中附加某种唯一标识符,然后PHP在进行替换之前只需搜索它?

为此使用simplehtml。

您可以将所有<h1>更改为foo,如下所示:

$html = file_get_html('http://www.google.com/');
foreach($html->find('h1') as $element) 
{
  $element->innertext = 'foo';
}
echo $html;
simplehtmldom框架允许您搜索和修改HTML文件或url的DOM。

http://simplehtmldom.sourceforge.net/

// Create DOM from URL or file $html =
file_get_html('http://www.google.com/');
// Find all images foreach($html->find('img') as $element)
        echo $element->src . '<br>';
// Find all links foreach($html->find('a') as $element)
       echo $element->href . '<br>';

另一个不错的库是querypath。它与jquery非常相似:

 qp($html_code)->find('body')->text('Hello World')->writeHTML();

https://fedorahosted.org/querypath/wiki/QueryPathTutorial