在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.

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.

Just use 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

Then you don't have to write it on your own