PHP查找并替换url和扩展名

我一直在寻找一个关于如何使用preg_replace或RegEX的示例</ p>

在HTML中搜索 http://www.mydomain.co.uk/page.html < / a>或/page.html


nn

并用.app替换.html </ p>

我只想替换属于的页面上的.html 这个网站</ p>

我有一个使用我们网站页面的应用程序。 当我从应用程序链接页面时,我将.app删除/更改格式。 哪个有效,我只想动态编辑页面上的链接。</ p>

干杯</ p>
</ div>

展开原文

原文

I've been looking for an example on how to use preg_replace or RegEX to

search HTML for either http://www.mydomain.co.uk/page.html or /page.html

and replace .html with .app

I only want to replace the .html on pages that belong to this site

I have an app that uses pages from our website. When I link page from the app I put .app so it removes/ changes the formatting. Which works, I just want to dynamically edit the links on the pages.

Cheers

dongluobei9359
dongluobei9359 你的问题不够明确。你不想在服务器端这样做吗?你尝试了什么?一些代码?
4 年多之前 回复

4个回答

OK for those interested, I've cracked it. It searches html for links then checks if the link belongs to site. Find ".html" replace with ".app" within the link Find old links replace with new links within html.

Thanks guys you pointed me in the right direction

function searchHTML($html){
// if the page is used by the app replace all domain pages .html with .app

        $domain ='mydomain.com';
        $regEX = '/<a[^>]+href=([\'"])(.+?)\1[^>]*>/I';
        preg_match_all($regEX, $html, $match); //check page for links
        for($l=0; $l<=count($match[2]); $l++){ //loop through links found
            $link = $match[2][$l];//url within href
            if ((strpos($link, $domain) !== false) || ($link[0]=="/")) { //check links are domain links(domain name or the first char is /)
                $updateLink = str_replace(".html", ".app", $link);//replace .html ext with .app ext
                $html = str_replace($link, $updateLink, $html); //update html with new links
            }       
        }        

    return $html;
}



  function changeURL($ url){
$ url = str_replace('html','app',$ url) ;
返回$ url;
}

echo changeURL('http://www.mydomain.co.uk/page.html');
</ code> </ pre>

< p>结果: http://www.mydomain.co.uk/page.app </ p>
</ div>

展开原文

原文

function changeURL($url) {
    $url = str_replace('html', 'app', $url);
    return $url;
}

echo changeURL('http://www.mydomain.co.uk/page.html');

Result: http://www.mydomain.co.uk/page.app

Try this, works with PHP 4 >= 4.0.5

<?php
    $string = "http://www.mydomain.co.uk/page.html";
    $string = preg_replace_callback("/mydomain.co.uk\/.+?(html)$/", "replace", $string);

    function replace($matches) {
        return str_replace("html", "app", $matches[0]);
    }

    echo $string;
du31992
du31992 虽然代码经常说不出话来,但最好为代码添加一些解释。 这会在审核队列中弹出,因为代码只有答案。
4 年多之前 回复
<?php

// it's important to use the $ in regex to be sure to replace only the suffix and not a part of the url

function change_url($url, $find, $replace){
    return preg_replace("#\\.".preg_quote($find, "#")."$#uim", ".".$replace, $url);
    }

echo change_url("http://www.mydomain.co.uk/page.html", "html", "app");

//retuns http://www.mydomain.co.uk/page.app

?>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐