找出字符串是否包含字符串,如果包含,就拆分它


Finding out if string contains string, and splitting it if it does

我想知道一个字符串是否包含多于1个http://

:

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

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

$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];
}

使用explosion

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

你也可以直接获取结果的一部分到变量中:

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