0

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.

3
  • The question is what is the pattern to the URLs? Commented Jun 27, 2013 at 3:01
  • @SH That's right. I will replace it with 'code'(preg_replace($patterns,$replacements,$urls);) Commented Jun 27, 2013 at 3:51
  • @SH $pattern should be a pattern of original url while $replacement should be a patter of url intended. Commented Jun 27, 2013 at 3:52

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Well....I need to get a pattern and change it with preg_replace. See comment on the question please and help me. :)
I am helping you. Whatever you're doing in your code above is just... wrong. If you encode the url data properly, you won't have to implement your own crazy brittle regexp solutions.
stackoverflow.com/questions/16746230/… Here is the history of my question. :) I am replacing one pattern to other since the url pattern I am getting from the feed does not really link a user to a right place. :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.