dis Posted August 26, 2008 Share Posted August 26, 2008 Hello, I currently have: <?php router::register('about/:id="/^\d/"/:title'); ?> I need to split the string by '/', unless '/' is found inside the ="" section. So, I need a function to split the above into an array which should match (hence why explode will not work): Array( [0] = about [1] = :id="/^\d/"; [2] = :title ) I can do this via array loops and strpos functions, but that seems slow and stupid. I am wondering if this can be done via a preg_match, returning matches. Any help would be fantastic. Cheers, - dis. Link to comment https://forums.phpfreaks.com/topic/121360-php-split-possible-preg_match-needed/ Share on other sites More sharing options...
redarrow Posted August 26, 2008 Share Posted August 26, 2008 try in_array function then.......... Link to comment https://forums.phpfreaks.com/topic/121360-php-split-possible-preg_match-needed/#findComment-625728 Share on other sites More sharing options...
joquius Posted August 26, 2008 Share Posted August 26, 2008 Uhh what does this mean: I need to split the string by '/', unless '/' is found inside the ="" section. and what do you want to get out of this: [1] = :id="/^\d/"; the string inside the double quotes? This kind of thing can be done really easily with preg_match I just need to know what you want to do. Link to comment https://forums.phpfreaks.com/topic/121360-php-split-possible-preg_match-needed/#findComment-625732 Share on other sites More sharing options...
dis Posted August 26, 2008 Author Share Posted August 26, 2008 Sorry, it is sort of confusing. All I need to get is an array. I am not worrying about anything inside the "" at the moment, just the process of splitting up the original string. Basically I need to split the string by /. However, if / is located inside the quotes ="", I don't want it to split. Some further examples: <?php router::register('about/:title'); Array( [0] = about [1] = :title ) router::register('about/:title="/"'); Array( [0] = about [1] = :title="/" ) router::register('about/:title/:id="/^\d/"); Array( [0] = about [1] = :title [2] = :id="/^\d/" ) ?> Those above are the results I am looking for. If you need further explaination, please let me know. Link to comment https://forums.phpfreaks.com/topic/121360-php-split-possible-preg_match-needed/#findComment-625734 Share on other sites More sharing options...
joquius Posted August 26, 2008 Share Posted August 26, 2008 Uhh preg_match_all ('/(:?.+?(?:=\".+?\")?)(?:\/|$)/s', $str, $matches); $str = 'about/:title="/"'; array(2) { [0]=> array(2) { [0]=> string(6) "about/" [1]=> string(10) ":title="/"" } [1]=> array(2) { [0]=> string(5) "about" [1]=> string(10) ":title="/"" } } Link to comment https://forums.phpfreaks.com/topic/121360-php-split-possible-preg_match-needed/#findComment-625745 Share on other sites More sharing options...
dis Posted August 26, 2008 Author Share Posted August 26, 2008 Thanks alot joquius. Much appreciated! Link to comment https://forums.phpfreaks.com/topic/121360-php-split-possible-preg_match-needed/#findComment-625747 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.