chazmus Posted January 4, 2010 Share Posted January 4, 2010 Hello, I'd like to be able to change a string that looks like /root/section/subsection/... (potentially alot more sections) so that $first = "root" $rest = "/section/subsection/" The string I gave is just an example, in reality there could be many different names for sections, and the first bit won't always be "root" and could be any length.. I have been googling for about an hour now and can't make any sense of regular expressions! Thanks if you can help Quote Link to comment Share on other sites More sharing options...
salathe Posted January 4, 2010 Share Posted January 4, 2010 As much a fan of regular expressions as I am (ask anyone here), sometimes basic string functions will suffice. Indeed, there are a number of different string functions that could be employed here to do the job! Coming to the rescue today is the under-rated and misunderstood strtok function. The snippet below takes the (x)path and hunts around for the first item and then "everything else", allocating those values to their appropriate variable. $path = '/root/section/subsection/etc'; $first = strtok($path, '/'); $rest = '/' . strtok(''); var_dump($first, $rest); If you really want a regular expression solution, or don't really feel like using strtok and would prefer other string functions, do let us know. Quote Link to comment Share on other sites More sharing options...
chazmus Posted January 4, 2010 Author Share Posted January 4, 2010 Amazing. strtok wins! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.