phpstripslashes

delete only first slashes from url/string


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 the http:// 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 correct http:// in front of these urls.

Maybe someone know a smart solution.

Thank you.


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