John Smith
John Smith

Reputation: 123

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

I want to figure out if a string contains more then 1 occurance of http://

Like this:

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

I know how to find out if it does, but how do I split the string at the begining of the second http?

Upvotes: 0

Views: 51

Answers (2)

Wouter J
Wouter J

Reputation: 41954

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

echo $secondPart;

More information in the documentation of explode


Or some other method (which is simpler and properbly faster):

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

Or you can also use a REGEX which I don't recommend, because it's to heavy for this simple task:

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

Upvotes: 1

Sumit Bijvani
Sumit Bijvani

Reputation: 8179

Use explode

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

You can also directly fetch parts of the result into variables:

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

Upvotes: 0

Related Questions