PHP的链接爆炸字符串

时间:2018-10-17 17:27:45

标签: php explode

我有带链接的字符串,例如-

$d = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/flight-formation.jpghttps://s3-us-west-2.amazonaws.com/s.cdpn.io/82/cat-nose.jpghttps://s3-us-west-2.amazonaws.com/s.cdpn.io/82/look-out.jpghttps://s3-us-west-2.amazonaws.com/s.cdpn.io/82/contrail.jpghttps://s3-us-west-2.amazonaws.com/s.cdpn.io/82/golden-hour.jpg';

如何爆炸字符串以使链接一一获得?

3 个答案:

答案 0 :(得分:0)

您需要具有某种分隔符才能使用explode。最好的选择可能是在“ http”上爆炸,然后将其还原到每个元素的前面。

答案 1 :(得分:0)

您可以使用explode('https://', $d);。但是我认为最好的方法是控制您第一次获得此字符串并添加到数组的方式。

答案 2 :(得分:0)

您可以使用str_replace在它们之间添加一个空格,并修剪str_replace创建的前导空间。
然后,您将获得一个可以像平常一样容易爆炸的字符串。

$d = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/flight-formation.jpghttps://s3-us-west-2.amazonaws.com/s.cdpn.io/82/cat-nose.jpghttps://s3-us-west-2.amazonaws.com/s.cdpn.io/82/look-out.jpghttps://s3-us-west-2.amazonaws.com/s.cdpn.io/82/contrail.jpghttps://s3-us-west-2.amazonaws.com/s.cdpn.io/82/golden-hour.jpg';
$d = trim(str_replace("http", " http", $d));
$arr = explode(" ", $d);
var_dump($arr);

输出:

array(5) {
  [0]=>
  string(68) "https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/flight-formation.jpg"
  [1]=>
  string(60) "https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/cat-nose.jpg"
  [2]=>
  string(60) "https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/look-out.jpg"
  [3]=>
  string(60) "https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/contrail.jpg"
  [4]=>
  string(63) "https://s3-us-west-2.amazonaws.com/s.cdpn.io/82/golden-hour.jpg"
}

https://3v4l.org/l7DRM