Monogot
Monogot

Reputation: 191

Split a string of URLs without delimiters before each new URL

I have string like this:

$test = http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg

And I want to split string to array like this:

Array(
  [0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jpg
  [1] => http://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)

What should I do?

Upvotes: 2

Views: 2471

Answers (7)

mickmackusa
mickmackusa

Reputation: 48100

preg_split() is an ideal tool for splitting these URLs. Use a lookahead to match the start of a URL without consuming it in the splitting process. Demo

$test = "http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg";

var_export(
    preg_split('#(?=http://)#', $test, 0, PREG_SPLIT_NO_EMPTY)
);

The pattern will split before the first URL, no it is important to filter out empty elements from the result. Preventing a split at the start could also be achieved with (?!^) at the start of the pattern.
preg_split('#(?!^)(?=http://)#', $test)

For matching http and https, use (?=https?://)

Upvotes: 0

ganji
ganji

Reputation: 864

For answering this question by regular expression I think you want something like this:

$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);

It returns exactly something you need:

Array
(
 [0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jp
 [1] => localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)

Upvotes: -1

user594138
user594138

Reputation:

$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);

Upvotes: 1

Spudley
Spudley

Reputation: 168853

You asked for a regex solution, so here you go...

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

The expression looks for parts of the string the start with http:// and end with .jpg, with anything in between. This splits your string exactly as requested.

output:

Array
(
    [0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jpg
    [1] => http://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)

Upvotes: 4

atwright147
atwright147

Reputation: 3821

Try the following:

<?php
$temp = explode('http://', $test);
foreach($temp as $url) {
    $urls[] = 'http://' . $url;
}
print_r($urls);
?>

Upvotes: 1

Antony
Antony

Reputation: 15114

$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);

Upvotes: 5

Modestas Stankevičius
Modestas Stankevičius

Reputation: 144

you can split them if they are always like this vith substr() function reference: http://php.net/manual/en/function.substr.php but if they are dynamic in lenght. you need to get a ; or any other sign that is not likely to be used there before 2nd "http://" and then use explode function reference: http://php.net/manual/en/function.explode.php $string = "http://something.com/;http://something2.com"; $a = explode(";",$string);

Upvotes: 1

Related Questions