string splitting into arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
noaksey2000
Forum Newbie
Posts: 5
Joined: Sun Aug 17, 2003 10:29 am

string splitting into arrays

Post 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.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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
Post Reply