0

I am trying to split a string at keyword "http://".

explode("http://", $input);

This doesn't work for me because it does not only split the $input but it also deletes "http://" from the string, which i don't want to happen.

What is the most effecient and fast way of doing this ? I didn't find any function for this so far.

dimitris93
  • 4,155
  • 11
  • 50
  • 86
  • 2
    where are the test cases? and the attempt?, whats the context of this anyway – Kevin Sep 23 '14 at 04:04
  • what do you mean test cases ? `explode("http://", "http://1.1http://2.2");` gives me `1.1` and `2.2` , i want to get `http://1.1` and `http://2.2` was it really not clear enough ? – dimitris93 Sep 23 '14 at 04:07
  • 3
    [**is-there-way-to-keep-delimiter-while-using-php-explode-or-other-similar-function**](http://stackoverflow.com/questions/2938137/is-there-way-to-keep-delimiter-while-using-php-explode-or-other-similar-function) – Mithun Satheesh Sep 23 '14 at 04:10
  • @Shiro nope i did not downvote, in this context, yes because after you exploded the delimiter will not be included – Kevin Sep 23 '14 at 04:10
  • @mithunsatheesh thanks for the link thats what i wanted – dimitris93 Sep 23 '14 at 04:11
  • You'd need to use some sort of regex pattern to pick it up and keep the delimeter – Darren Sep 23 '14 at 04:12
  • There are some idiots on this site who are just playing the idiotic repo game..!! I think they are getting paid for down voting other queries and answers. isn't it..?? – Ritesh Sep 23 '14 at 04:48

1 Answers1

1

A bit of regex lookahead should do it:

<?php

$input = "The address is http://stackoverflow.com/";
$parts = preg_split('@(?=http://)@', $input);
var_dump($parts);

This would output:

array(2) {
  [0]=>
  string(14) "The address is"
  [1]=>
  string(25) "http://stackoverflow.com/"
}

Hope that helps!

wavemode
  • 2,076
  • 1
  • 19
  • 24