繁体   English   中英

在PHP中使用正则表达式更改URL模式

[英]changing url pattern using regular expression in php

我想我对PHP完全不满意。

我正在尝试将网址格式更改为其他格式

http://da.feedsportal.com/c/34762/f/640634/s/2dd0817c/l/0L0Sexample0Bco0Bkr0Carti0Cpolitics0Cassembly0C5933970Bhtml/ia1.htm

http://www.example.co.kr/arti/politics/assembly/593397.html

因此,我只从原始网址中获取“ example0Bco0Bkr0Carti0Cpolitics0Cassembly0C5933970Bhtml”,然后对“ 0B”->“”进行了一些调整。 和'0C'->'/'

应当删除其他部分,例如da.feedsportal.com/c/34762/f/640634/s/2dd0817c/l/0L0S和/ia1.htm。

任何帮助将不胜感激。

只需使用urlencode

echo urlencode("http://www.example.co.kr/arti/politics/assembly/593397.html");
//=> http%3A%2F%2Fwww.example.co.kr%2Farti%2Fpolitics%2Fassembly%2F593397.html

那你就不用自己写

使用strposstrrpossubstrstr_replace 我们将分四个主要步骤进行。 查找子字符串的开始和结束。 验证。 抓住子串。 转换子字符串。

基本上:

function convert ($inputString) {
    $start = strpos( $inputString, '0L0S' );
    $end = strrpos( $inputString, '/' );

    if(! $start){
        return false;
    }

    $start = $start + 4; // this is because strpos gives start of found string
    $length = $end - $start;
    $innerString = substr( $inputString, $start, $length);

    $pass1 = str_replace ('0B', '.', $innerString);
    $pass2 = str_replace ('0C', '/', $pass1);

    return $pass2;
}

我敢肯定有一种很不错的方法。 但这应该可行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM