Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用SimpleHtmlDom,如何删除和替换特定属性_Php_Parsing_Simple Html Dom - Fatal编程技术网

Php 使用SimpleHtmlDom,如何删除和替换特定属性

Php 使用SimpleHtmlDom,如何删除和替换特定属性,php,parsing,simple-html-dom,Php,Parsing,Simple Html Dom,我目前正在使用这个使用php的HTML DOM解析器: 我对如何删除和替换所选属性感到困惑,我想用“index/style.css”替换链接,是否只插入 索引/ 或者从整个html代码中替换整个属性 官方手册中有几个示例基本上涵盖了您需要的所有内容: 如果您对某些特定步骤有疑问,请随时更新您的问题并提供一些代码。官方手册中有几个示例,基本上涵盖了您需要的所有内容: 如果您对某些特定步骤有疑问,请随时更新您的问题并提供一些代码。这应该可以做到: $doc = str_get_html($cod

我目前正在使用这个使用php的HTML DOM解析器:

我对如何删除和替换所选属性感到困惑,我想用
“index/style.css”
替换链接,是否只插入

索引/


或者从整个html代码中替换整个属性

官方手册中有几个示例基本上涵盖了您需要的所有内容:


如果您对某些特定步骤有疑问,请随时更新您的问题并提供一些代码。

官方手册中有几个示例,基本上涵盖了您需要的所有内容:

如果您对某些特定步骤有疑问,请随时更新您的问题并提供一些代码。

这应该可以做到:

$doc = str_get_html($code);
foreach ($doc->find('a[href]') as $a) {
    $href = $a->href;
    if (/* $href begins with a relative URL path */) {
        $a->href = 'index/'.$href;
    }

}
$code = (string) $doc;
您还可以使用:

这应该做到:

$doc = str_get_html($code);
foreach ($doc->find('a[href]') as $a) {
    $href = $a->href;
    if (/* $href begins with a relative URL path */) {
        $a->href = 'index/'.$href;
    }

}
$code = (string) $doc;
您还可以使用:


为什么不使用原生PHP DOM解析器?什么是原生PHP DOM解析器?@dqhendricks PHP DOM库提供了相当粗糙的功能集。您需要自己编写许多基本特性。如果第三方库足够可靠,那么它们是一个不错的选择。为什么不使用本机PHP DOM解析器?什么是本机PHP DOM解析器?@dqhendricks PHP DOM库提供了一个相当粗糙的功能集。您需要自己编写许多基本特性。如果第三方库足够可靠,那么它们是一个不错的选择。
$html = str_get_html($string); 
if ($html){ // Verify connection, return False if could not load the resource
    $e = $html->find("a");
    foreach ($e as $e_element){
        $old_href = $e_element->outertext;
        // Do your modification in here 
        $e_element->href = affiliate($e_element->href); // for example I replace original link by the return of custom function named 'affiliate'
        $e_element->href = ""; //remove href
        $e_element->target .= "_blank"; // I added target _blank to open in new tab
        // end modification 
        $html = str_replace($old_href, $e_element->outertext, $html); // Update the href
    }