0

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?

2
  • With strrpos and substr. Or simpler: a regex looking for the first occurence and stripping until there. Commented Mar 17, 2013 at 16:39
  • @mario a REGEX simpler? Please no. It's slower and really not made of tasks as this one Commented Mar 17, 2013 at 16:41

2 Answers 2

1
$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];
}
Sign up to request clarification or add additional context in comments.

6 Comments

Ty, looking in to explode now
The explode (totally overused) approach results in three parts. And yes, three lines of code are more effort than one (regex).
@mario more effort to type, but less time to render... (however, there are just 2 lines...) I've also added a second example, which is just one line and maybe the fastest option.
@mario to make you happy, I also added a REGEX example
For small strings, yes. Also I'm very happy. (But btw, I believe OP may have wanted to extract the second URL.)
|
0

Use explode

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

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

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

1 Comment

It'll be three parts with the whacky explode approach, the first being an empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.