doumen6605 2017-08-30 18: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 19: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

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

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题