JPashs
JPashs

Reputation: 13886

Replace text in a string using PHP

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'

Upvotes: 1

Views: 109

Answers (3)

Jay Blanchard
Jay Blanchard

Reputation: 34416

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.

Upvotes: 1

Noman
Noman

Reputation: 4116

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
        )

)

Upvotes: 0

FareedMN
FareedMN

Reputation: 142

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;
?>

Upvotes: 3

Related Questions