PHP头条
热点:

php字符串将http替换为https(是否带有www)



我有一个这样的字符串:

$url = '

IMAGE 1:


IMAGE 2:


IMAGE 3:


IMAGE 4:

'

example.com是我的Web应用程序所在的域。

我需要:


  1. 检查example.com是http还是https

  2. 如果使用https,请将我的字符串中的所有http://example.com和http://www.example.com替换为:https://www.example.com(因此不包括其他域,例如externalsite.com)

对于第一点,我可以使用:$isHttps = !empty($_SERVER['HTTPS']) ? true : false;

第二点,我不确定如何正确创建preg_replace


我认为这是您想要的逻辑。我们可以搜索以下正则表达式模式:

http://(?:www\.)?example\.com

这将同时匹配http://example.comhttp://www.example.com。然后,我们可以用这些网址的https版本代替。

$url = '

IMAGE 1:


IMAGE 2:


IMAGE 3:


IMAGE 4:

';
$output = preg_replace("/http:\/\/(?:www\.)?example\.com/","https://www.example.com",$url);
echo $output;

此打印:

IMAGE 1:


IMAGE 2:


IMAGE 3:


IMAGE 4:



www.phpzy.comtrue/phpzx/50036.htmlTechArticlephp字符串将http替换为https(是否带有www) 我有一个这样的字符串: $url = ' IMAGE 1: IMAGE 2: IMAGE 3: IMAGE 4: ' example.com是我的Web应用程序所在的域。 我需要: 检查example.com是http还是https 如果...

相关文章

PHP之友评论

今天推荐