i have a php variable which contain url string like this:
$url1 = 'http://test1.com/';
$url2 = 'https://test2.com';
$url3 = 'http://test3.com/';
i want to replace the http or https prefix with specific string and remove the dash at the end only if the string variable contain dash at the end of the string, for example:
$url1 = 'stackoverflow://test1.com';
$url2 = 'stackoverflow://test2.com';
$url3 = 'stackoverflow://test3.com';
You can try like:
$test = "https://stackoverflow.com/";
$test = rtrim(preg_replace ('/https|http/','test',$test,1),'/');
echo $test;
You can just use str_replace to solve this,
$url = rtrim(str_replace(['http://', 'https://', ], 'stackoverflow://', $url), '/');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With