1

I have this string:

http://scontent-b.xx.fbcdn.net/hphotos-prn2/t1/c22.0.100.100/p100x100/1489204_568091786618712_2075358603_n.jpg

I want to remove following part of the string:

c22.0.100.100/p100x100/

... But following part of the string is dynamic (changes):

c22.0.100.100

I'm thinking that it might be possible to use the PHP preg_ function in combination with a regular expression i some way? Example: Remove backwards from /p100x100/ to next / ??

Does anyone have a solution for this problem?

4
  • 3
    you know the string you want to remove is alway starts at the 5th / and ends at the 7th. should take you less than 10 seconds to come up with a solution from there without even bothering up with a regex Commented Jan 21, 2014 at 15:05
  • 1
    You must give more examples of how c22.0.100.100 can change Commented Jan 21, 2014 at 15:06
  • I have a solution. But this is basic, try out some regexes (if you need them, explode() should suffice) and come back with code you've written Commented Jan 21, 2014 at 15:06
  • Thank you for quick replies guys. Can you come up with solutions? Im not very familiar with regex's. Casimir another example could be: c170.0.100.100 and c19.0.100.100 Commented Jan 21, 2014 at 15:14

4 Answers 4

1

CODE:

  $a = 'http://scontent-b.xx.fbcdn.net/hphotos-prn2/t1/c22.0.100.100/p100x100/1489204_568091786618712_2075358603_n.jpg';;
  $b = preg_replace("/[^\/]*\/p100x100\//",'',$a);
  echo 'A: '.$a."\n";
  echo 'B: '.$b."\n";

This regex [^\/]*\/p100x100\/ replaces...

  • [^\/]* as many non-'/'
  • \/p100x100\/ followed by /p100x100/
  • '' with nothing

OUTPUT:

A: http://scontent-b.xx.fbcdn.net/hphotos-prn2/t1/c22.0.100.100/p100x100/1489204_568091786618712_2075358603_n.jpg
B: http://scontent-b.xx.fbcdn.net/hphotos-prn2/t1/1489204_568091786618712_2075358603_n.jpg
Sign up to request clarification or add additional context in comments.

Comments

0
preg_replace('¦/t1/.*/p100x100/¦', '/t1/', $string)

could throw some funny problems though using .* but without any other information on what the replaceable string could be it's the best that can be done.

what's your final aim of this? what does it do, what does it need to do, and why, these are all pieces of the puzzle that will help people answer :)

2 Comments

Thank you Ben. I'll be back tomorrow.. "/t1/" isnt always part of the string :-(
preg_replace('~[^/]+/p100x100/~', '', $string); I haven't tried it out, but it might work to remove those two directories. It might require some tweaking.
0

Do like this to remove the c22.0.100.100/p100x100/

<?php
$str="http://scontent-b.xx.fbcdn.net/hphotos-prn2/t1/c22.0.100.100/p100x100/1489204_568091786618712_2075358603_n.jpg";

    echo preg_replace('~(?<=t1).*(?=\/)~',"",$str);

Demo

Comments

0

How about:

preg_replace('~/c\d+(?:\.\d+){3}/p100x100\b~', '', $str)

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.