替换 PHP 字符串中的 http 或 https 前缀

Tin*_*cer 2 php regex string

我有一个 php 变量,其中包含如下 url 字符串:

$url1 = 'http://test1.com/';
$url2 = 'https://test2.com';
$url3 = 'http://test3.com/';
Run Code Online (Sandbox Code Playgroud)

我想用特定字符串替换 http 或 https 前缀,并仅当字符串变量在字符串末尾包含破折号时删除末尾的破折号,例如:

$url1 = 'stackoverflow://test1.com';
$url2 = 'stackoverflow://test2.com';
$url3 = 'stackoverflow://test3.com';
Run Code Online (Sandbox Code Playgroud)

Emp*_*ain 6

你可以尝试像:

$test = "https://stackoverflow.com/";
$test = rtrim(preg_replace ('/https|http/','test',$test,1),'/');
echo $test;
Run Code Online (Sandbox Code Playgroud)