找出string是否包含字符串,如果包含字符串则将其拆分

时间:2013-03-17 16:35:36

标签: php string

我想弄清楚一个字符串是否包含多于1个http://

的出现

像这样:

http://uploading.com/files/c8e99378/image-slider-skins.zip/http://www.filesonic.com/file/3524497744/image-slider-skins.zip

我知道如何判断它是否存在,但如何在第二个http的开头分割字符串?

2 个答案:

答案 0 :(得分:1)

$parts = explode('http://', $str);
$secondPart = 'http://'.$parts[2];

echo $secondPart;

explode

文档中的更多信息

或其他一些方法(更简单,更快速):

$firstPart = substr($str, 0, strpos($str, 'http://', 8));

或者你也可以使用我不推荐的REGEX,因为这个简单的任务很重要:

if (preg_match('/(http:\/\/.*)(?=http:\/\/)/', $str, $matches)) {
    echo $matches[1];
}

答案 1 :(得分:0)

使用explode

$parts = explode('http://', $string);

您也可以直接将部分结果提取到变量中:

list($part1, $part2) = explode('http://', $string);