dsfsad089111 2013-06-27 02:48
浏览 19
已采纳

在php中使用正则表达式更改url模式

I guess I am a total noobie on PHP.

I am trying to change a url pattern to another pattern

from

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

to

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

so I am only taking "example0Bco0Bkr0Carti0Cpolitics0Cassembly0C5933970Bhtml" from the original url and tweak a bit '0B' -> '.' and '0C' -> '/'

Other parts like da.feedsportal.com/c/34762/f/640634/s/2dd0817c/l/0L0S and /ia1.htm shall be removed.

Any help would be highly appreciated.

  • 写回答

2条回答 默认 最新

  • dongxingchang9345 2013-06-27 21:12
    关注

    Use strpos , strrpos, substr and str_replace. Were gonna take it in 4 main steps. Find start and end of substring. A touch of validation. Grab the substring. Convert substring.

    Basicly:

    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;
    }
    

    I'm sure there is a fancy way of doing this. But this should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效