3

我是 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
)

我应该怎么办?

4

6 回答 6

5
$test = 'http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg';
$testurls = explode('http://',$test);
foreach ($testurls as $testurl) {
    if (strlen($testurl)) // because the first item in the array is an empty string
    $urls[] = 'http://'. $testurl;
}
print_r($urls);
于 2013-06-07T09:25:55.173 回答
4

你要求一个正则表达式解决方案,所以你去......

$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
)
于 2013-06-07T09:29:29.763 回答
1

尝试以下操作:

<?php
$temp = explode('http://', $test);
foreach($temp as $url) {
    $urls[] = 'http://' . $url;
}
print_r($urls);
?>
于 2013-06-07T09:27:17.620 回答
1

如果它们总是像这样的 vith substr() 函数参考:http://php.net/manual/en/function.substr.php 但如果它们在长度上是动态的,则可以拆分它们。您需要;在第二个“http://”之前获得一个或任何其他不太可能在那里使用的标志,然后使用爆炸功能参考: http: //php.net/manual/en/function.explode.php $string = "http://something.com/;http://something2.com"; $a = explode(";",$string);

于 2013-06-07T09:25:13.177 回答
1
$test = 'http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jp';

array_slice(
    array_map(
        function($item) { return "http://" . $item;}, 
        explode("http://",  $test)), 
    1);
于 2013-06-07T09:29:56.640 回答
0

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

$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
)
于 2015-12-27T13:39:21.530 回答