在php中使用正则表达式拆分字符串

在php中使用正则表达式拆分字符串,php,regex,Php,Regex,我是php初学者,我有如下字符串: $test = http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg 我想将字符串拆分为如下数组: Array( [0] => http://localhost/biochem/wp-content/uploads//godzil

我是php初学者,我有如下字符串:

$test = http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
我想将字符串拆分为如下数组:

Array(
[0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jpg
[1] => http://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)
$test = "http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg";
    $keywords = preg_split("/.http:\/\//",$test);
    print_r($keywords);

我该怎么办?

如果它们总是像下面的vith substr()函数引用那样,那么可以拆分它们:但是如果它们在长度上是动态的。你需要一个
或在第二个“http://”之前不可能在此处使用的任何其他符号,然后使用explode函数引用:
$string=”http://something.com/;http://something2.com"; $a=分解(“;”,$string)

请尝试以下操作:

<?php
$temp = explode('http://', $test);
foreach($temp as $url) {
    $urls[] = 'http://' . $url;
}
print_r($urls);
?>

您要求一个正则表达式解决方案,所以给您

$test = "http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg";
preg_match_all('/(http:\/\/.+?\.jpg)/',$test,$matches);
print_r($matches[0]);
表达式查找字符串的一部分,以
http://
开头,以
.jpg
结尾,中间有任何内容。这会完全按照要求拆分字符串

输出:

Array
(
    [0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jpg
    [1] => http://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)

为了用正则表达式回答这个问题,我想你需要这样的东西:

Array(
[0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jpg
[1] => http://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)
$test = "http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg";
    $keywords = preg_split("/.http:\/\//",$test);
    print_r($keywords);
它返回的正是您需要的内容:

Array
(
 [0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jp
 [1] => localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)

+1用于实际使用正则表达式。但是这需要
.jpg
在结尾处。@Antony-是的,但是OP的输入包含了这一点。鉴于缺乏可靠的标准,我将其建立在问题的基础上。