0

Sorry, I'm bad English. I'm going to post my code now:

    $image = 'http://example.com/thisisimage.gif';
    $filename = substr($image, strrpos($image, '/') + 1);
    echo '<br>';
    echo $filename;
    echo '<br>';            
    echo preg_replace('/^[^\/]+/', 'http://mydomain.com', $image);   
    echo '<br>';    

$image is string;

$filename is image name (in example above, it returns 'thisisimage.gif')

Now i want replace all before $filename with 'http://mydomain.com', my code is above but it doesnt work.

Thanks!

3
  • do you want to turn //example.com/thisisimage.gif to //localhost/thisisimage.gif ? Really I did not understand what you are asking. Commented Mar 15, 2013 at 9:35
  • @Ihsan: yes! i have edited, is 'mydomain.com'. Commented Mar 15, 2013 at 9:37
  • Then you have allready done the hard part. Look at my answer below. Commented Mar 15, 2013 at 9:43

6 Answers 6

2
$foo = explode($filename, $image);
echo $foo[0];

Explode "splits" one the given paramater ( in your case $filename ). It returns an array with where the keys are split on the string you gave.

And if you just want to change the url. you use a str_replace

   $foo = str_replace("http://example.com", "http://localhost", $image);

   //This will change "http://example.com" to "http://localhost", like a text replace in notepad.

In your case:

    $image = 'http://example.com/thisisimage.gif';
    $filename = substr($image, strrpos($image, '/') + 1);
    $foo = explode($filename, $image);
    echo '<br>';
    echo $filename;
    echo '<br>';            
    echo str_replace($foo[0], "http://yourdomain.com/", $url);
    echo '<br>';   
Sign up to request clarification or add additional context in comments.

1 Comment

i think this is the best solution. Thanks!
2

There's another approach in which you don't need a regular expression:

in Short:

$image = 'http://example.com/thisisimage.gif';
$url = "http://mydomain.com/".basename($image);

Explanation:

If you just want the file name without url's or directory path's, basename() is your friend;

$image = 'http://example.com/thisisimage.gif';
$filename = basename($image);

output: thisisimage.gif

Then you can add whatever domain you want:

$mydomain = "http://mydomain.com/";
$url = $mydomain.$filename;

1 Comment

dont forget the "/" basename() just grabs the filename.
1

Try this :

$image = 'http://example.com/thisisimage.gif';  
echo preg_replace('/^http:\/\/.*\.com/', 'http://mydomain.com',$image);

Comments

1

This should simply work:

$image = 'http://example.com/thisisimage.gif';
$filename = substr($image, strrpos($image, '/') + 1);
echo '<br>';
echo $filename;
echo '<br>';            
echo 'http://mydomain.com/'.$filename;   
echo '<br>';    

Comments

1

if you just like to add your own domain before the file name, try this:

$filename = array_pop(explode("/", $image));
echo "http://mydomain.com/" . $filename;

if you wanna only replace thedomain, try this:

echo preg_replace('/.*?[^\/]\/(?!\/)/', 'http://mydomain.com/', $image);

Comments

1

The other people here have given good answers about how to do it - regex has its advantages but also drawbacks - its slower, respectively requires more resources and for something simple as this, I would advice you to use the explode approach, but while speaking for regex functions you also may try this, instead your preg_replace:

echo preg_replace('#(?:.*?)/([^/]+)$#i', 'http://localhost/$1', $image);

It seems variable length positve lookbehind is not supported in PHP.

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.