doumen6605 2017-08-30 10:53
浏览 9
已采纳

2查找和追加的PHP条件

I have written a PHP code that fetches website images from my other website (both are on separate hosting)

$listings['pic'][$count] = $critem[2];  

The $critem[2] sometimes returns either 1 or 2

1. https://www.example.com/wp-content/plugins/featured-content-gallery/scripts/mootools.v1.11.js

2. /wp-content/uploads/2017/07/Sidetracks-Main-1.jpg

What I am trying to do is Find

http://www.example.com and replace with https://www.example.com and if there is no domain in the $critem[2] as in the (2nd example) append https://www.example.com to the string

I am stuck as str_replacedoes not work in this criteria

  • 写回答

1条回答 默认 最新

  • douzi8127 2017-08-30 11:05
    关注

    Something like this?

    $arr = ["http://www.example.com//wp-content/uploads/2017/07/Sidetracks-Main-1.jpg", "/wp-content/uploads/2017/01/45432.jpg", "/wp-content/uploads/2016/07/39223.jpg"];
    
    foreach($arr as &$link){
        $link = str_replace("http://", "https://", $link);
        if(substr($link,0,8) != "https://") $link = "https://www.example.com" . $link;
    }
    
    Var_dump($arr);
    

    It replaces http with https if it's in the string.
    And if the link does not have the http, it adds it with the str_replace
    https://3v4l.org/G0AiQ

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部