1

I have this string:

$my_string = "http://apinmo.com/1/2/3.jpg 4444/8888/7777  http://apinmo.com/4/5/8-1.jpg";

And I need to remove the last two slashs of the urls to get this, all the rest remain the same:

$my_NEW_string = "http://apinmo.com/123.jpg 4444/8888/7777   http://apinmo.com/458-1.jpg";

I tried this:

$my_NEW_string = preg_replace('/(?<=\d)\/(?=\d)/', '', $my_string);

but I get this:

$my_NEW_string = "http://apinmo.com/123.jpg 444488887777   http://apinmo.com/458-1.jpg";

The slashes in 4444/8888/7777 where removed and this not what I need. They must to remain there.

UPDATE: due to the context where this code is used I need this approach: make replacements between 'http' and 'jpg'

5
  • why dont you try to get only images url from string ? Commented Feb 11, 2016 at 13:55
  • Are the spaces in the string important? Commented Feb 11, 2016 at 13:55
  • @JayBlanchard there are spaces in the string. Yes, they shoulb remain as it. Commented Feb 11, 2016 at 13:57
  • You can replace since a position in your string. ie from the second space or from the second http position to the end. Commented Feb 11, 2016 at 13:57
  • This could help: I only need to make replacements between 'http' and 'jpg'. Commented Feb 11, 2016 at 13:59

3 Answers 3

3

This is an example you might use :

<?php

$my_string = "http://apinmo.com/1/2/3.jpg 4444/8888/7777 http://apinmo.com/4/5/8-1.jpg";
$my_array= split(" ",$my_string); // split it to 3 strings 
$part1=preg_replace('/(?<=\d)\/(?=\d)/', '', $my_array['0']);
$part2=$my_array['1'];
$part3=preg_replace('/(?<=\d)\/(?=\d)/', '', $my_array['2']);

$my_new_string=$part1." ".$part2." ".$part3;
?>
Sign up to request clarification or add additional context in comments.

5 Comments

This doesn't preserve the spaces.
What if I have more parts? It will fail if the space is missing.
I'm not sure i completely understand what you mean by preserve but when you get the three parts you can add as much spaces as you want
this is an example , if you have basic php knowledge you can alter it really easy
I need other approach, like making the replacements between 'http' and 'jpg'
1

Here is yet another way to do this and preserve your spaces:

$my_string = "http://apinmo.com/1/2/3.jpg 4444/8888/7777  http://apinmo.com/4/5/8-1.jpg";
$string_array = explode(' ', $my_string);
print_r($string_array); // for testing

$new_array = '';
foreach($string_array AS $original) {
    $pos = strpos($original, 'http');
    if(0 === $pos){
        $new = preg_replace('/(?<=\d)\/(?=\d)/', '', $original);
        $new_array[] = $new;
    } else {
        $new_array[] = $original;
    }
}
$new_string = implode(' ', $new_array);
echo $new_string;

Returns (note the preserved spaces):

http://apinmo.com/123.jpg 4444/8888/7777  http://apinmo.com/458-1.jpg

EDIT - Pure regex method:

$new_string = preg_replace('/(?<=\/\d)(\/)/', '', $my_string);
echo $new_string;

Returns: http://apinmo.com/123.jpg 4444/8888/7777 http://apinmo.com/458-1.jpg

CAVEATS: a. ) works even if there are no spaces in the string 2. ) does not work if any number between / is more than one digit in length. iii. ) if the second group of digits is like 4444/5/8888 the second slash would get removed here too.

Here is how the regex breaks down:

Using a positive lookbehind to match a / followed by a digit (?<=\/\d) I can assert what I am looking for - I only want to remove the forward slashes after a forward slash followed by a digit. Therefore I can capture the other forward slashes with (\/) immediately after the lookbehind. There is no need to include http:// to start or .jpg to close out.

5 Comments

Thanks but it is based in "space". Due to the context where this code is used I need this approach: make replacements between 'http' and 'jpg'
That is not what you originally asked for @canelones
If there are spaces then they should remain, that is what I meant, but spaces might not be present, then I can not use them to do this.
I see what you mean. I will see if I can hack together a solution.
Is it always single digits between the / @canelones?
0

If the strings always has images you will get all images in $matches array.

As per your comment you need

This could help: I only need to make replacements between 'http' and 'jpg'.

You can make replace by playing with array as per the requirement.

$input = 'http://apinmo.com/123.jpg 4444/8888/7777   http://apinmo.com/458-1.jpg';
preg_match_all('(https?:\/\/\S+\.(?:jpg|png|gif))', $input, $matches);

echo '<pre>';print_r($matches);echo '</pre>';

Output:

 Array
(
    [0] => Array
        (
            [0] => http://apinmo.com/123.jpg
            [1] => http://apinmo.com/458-1.jpg
        )

)

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.