【问题标题】:Replace link-ending of specific URLs替换特定 URL 的链接结尾
【发布时间】:2018-09-24 08:15:42
【问题描述】:

我想更改已解析 html 中特定链接的 URL 结尾。在以前的场合我使用过这段代码

$html = file_get_html('URL');
// search for .htm change to .php
foreach ($html->find("td a") as $a) {
    $a->href = substr_replace($a->href, 'php', -3);

但它会改变所有的链接。我现在必须区分特定的 URL。我只想更改那些在其链接中包含“框”的链接。

Example:
thisismyboxlink123.htm --> no change of ending please
thisismyotherlink123.htm --> thisismyotherlink123.php  

【问题讨论】:

    标签: php url replace


    【解决方案1】:

    您可以使用strpos() 来检查字符串"box" 是否在链接内。如果不是(===false),你可以替换它:

    $html = file_get_html('URL');
    // search for .htm change to .php
    foreach ($html->find("td a") as $a) {
        if (strpos($a->href, 'box') === false) {
            $a->href = substr_replace($a->href, 'php', -3);
        }
    }
    

    【讨论】:

    • 完美。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多