Page 1 of 1

string splitting into arrays

Posted: Sat Apr 17, 2004 9:41 am
by noaksey2000
hey up people.

right i have a string (a link) to a certain picture.
for example:

"pictures/1/1/3.jpg"

and what i want to do is to split it at the second slash so that everything to the left ('pictures/1/')goes in array0 and everything to the right ('1/3.jpg')goes in array1. is this possible at all?! im tryin to find easier ways other than explode, joining them all together, etc.

cheers for any help,
chris.

Posted: Sat Apr 17, 2004 10:47 am
by redmonkey
Dependant on the string your trying to match this may work...

Code: Select all

<?php

$string_in = '<a href="pictures/1/1/3.jpg">';

preg_match('/([^"]+\w+\/\d+\/)(\d+\/[^"]+)/', $string_in, $match);

print_r($match);

?>
Will result in...

Code: Select all

Array
(
    &#1111;0] =&gt; pictures/1/1/3.jpg
    &#1111;1] =&gt; pictures/1/
    &#1111;2] =&gt; 1/3.jpg
)
[0] = Entire match part of the string
[1] = Everything upto and including second slash
[2] = the remaining part of the link