php - delete only first slashes from url/string

829

I want to delete the first two slashes of my string.

//cdn.klingel.de/images/100/3/7/1/5/0/8/371508F1.jpg

After that I want to include thehttp:// new. But that isn't the problem.

str_replace replace all slashes...

Info:

I've different strings. Examples:

/media/images/CmsPageModuleDataItem/62/6260.0_gefro-suppennudeln.jpg

//cdn.abc.de/images/100/3/7/1/5/0/8/371508F1.jpg

http://s7.abc.com/is/image/LandsEnd/461469_FG16_LF_616

I need a correcthttp:// in front of these urls.

Maybe someone know a smart solution.

Thank you.

638

Answer

Solution:

Hope this is this what you are looking for.

$str = "//Hello World/theEnd!";
echo $str . "<br>";
$str = trim($str,"/");
$str = "http://" . $str; 
echo $str;

gives youhttp://Hello World/theEnd!

If you want to get fancy you can put this is a loop as well.

function addhttp($url) {
$url = trim($url,"/");

if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
    $url = "http://" . $url;
}
return $url;

}

$add = "/media/images/CmsPageModuleDataItem/62/6260.0_gefro-suppennudeln.jpg";
$add1 = "//cdn.abc.de/images/100/3/7/1/5/0/8/371508F1.jpg";
$add2 = "http://s7.abc.com/is/image/LandsEnd/461469_FG16_LF_616";
echo "<br />"; 

echo addhttp($add); 
echo "<br />"; 
echo addhttp($add1); 
echo "<br />"; 
echo addhttp($add2); 

this gives you

http://media/images/CmsPageModuleDataItem/62/6260.0_gefro-suppennudeln.jpg
http://cdn.abc.de/images/100/3/7/1/5/0/8/371508F1.jpg
http://s7.abc.com/is/image/LandsEnd/461469_FG16_LF_616
644

Answer

Solution:

Another option is to use regex for this:

^((http:)?(\/){1,2}).*

Here is a regex101 fiddle:
https://regex101.com/r/lUXTDf/1

And this is a usage in php using preg_replace:

var_dump(preg_replace('/^(?:(?:http:)?(?:\/){1,2})(.*)/', 'http://\1', $s1));

http://sandbox.onlinephpfunctions.com/code/b49af5519e8a7a4e47baffa9a8a7199c3bddbd42

334

Answer

Solution:

A strong way is to useparse_url and to rebuild the url part by part:

$urls = [
    '/media/images/CmsPageModuleDataItem/62/6260.0_gefro-suppennudeln.jpg',
    '//cdn.abc.de/images/100/3/7/1/5/0/8/371508F1.jpg',
    'http://s7.abc.com/is/image/LandsEnd/461469_FG16_LF_616',
    'toto',
    'example.com/path?a=1&b=2#anchor'
];

$results = [];

define ('DEFAULT_SCHEME', 'http');
define ('DEFAULT_HOST', 'default.host.com');

foreach ($urls as $url) {
   $parts = parse_url($url);

   $results[] = (isset($parts['scheme']) ? $parts['scheme'] : DEFAULT_SCHEME) . '://'
              . (isset($parts['host'])? $parts['host'] : DEFAULT_HOST)
              . '/' . ltrim($parts['path'], '/')
              . (isset($parts['query']) ? '?' . $parts['query'] : '')
              . (isset($parts['fragment']) ? '#' . $parts['fragment'] : '');
}

print_r($results);

demo

382

Answer

Solution:

You can use the explode function:

<?php
    $str = "//cdn.klingel.de/images/100/3/7/1/5/0/8/371508F1.jpg";
    $array= explode("/",$str);
?>

then you can make a forloop to put the array back into a string.

http://www.w3schools.com/php/func_string_explode.asp

845

Answer

Solution:

@cgee use ltrim() function it will trim left side means you starting string's / or // both just look try the below one

<?php
    $preString = "http://";
    $s1 = "/media/images/CmsPageModuleDataItem/62/6260.0_gefro-suppennudeln.jpg";
    $s2 = "//cdn.abc.de/images/100/3/7/1/5/0/8/371508F1.jpg";
    echo $preString.ltrim($s1, '/');
    echo "<br>";
    echo $preString.ltrim($s2, '/');
?>

People are also looking for solutions to the problem: Joining three tables using MySQL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.