proggR Posted July 12, 2008 Share Posted July 12, 2008 Is there anyway to break apart a string into groups? I have a string thats formatted DATE_COURSE_TITLE and i was wondering if there's a function that can grab any characters after the second underscore? And I don't need this to use but for knowledge sake, is there anyway to grab just between the two underscores as well? I'm guessing something like: $i = 0 if (character == "_"){ getcharacter; if (character == "_"){ $word = $buffer; } else{ $buffer[$i] = getcharacter; $i = $i+1; } would work but i was wondering if there was a function that would do it. even if i have to find the placement of each underscore and just feed start and end points into the function or something. althought i guess i could write that myself. anyway. i'm rambling because I'm in a hurry. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/114366-solved-break-apart-strings/ Share on other sites More sharing options...
DarkWater Posted July 12, 2008 Share Posted July 12, 2008 $string = "DATE_COURSE_TITLE"; $new = explode("_", $string); $new[0] is "DATE", $new[1] is "COURSE, $new[2] is "TITLE". Link to comment https://forums.phpfreaks.com/topic/114366-solved-break-apart-strings/#findComment-588110 Share on other sites More sharing options...
mbeals Posted July 12, 2008 Share Posted July 12, 2008 Look up preg_match, Preg_match_all and preg_split for even more ways of hacking strings apart Link to comment https://forums.phpfreaks.com/topic/114366-solved-break-apart-strings/#findComment-588129 Share on other sites More sharing options...
DarkWater Posted July 12, 2008 Share Posted July 12, 2008 He just needs explode(). Link to comment https://forums.phpfreaks.com/topic/114366-solved-break-apart-strings/#findComment-588131 Share on other sites More sharing options...
mbeals Posted July 12, 2008 Share Posted July 12, 2008 He was asking for knowledge sake, so I thought I'd expand his knowledge by pointing him toward some more advanced functions. But yes, use explode if you can and save the regex for special cases. Link to comment https://forums.phpfreaks.com/topic/114366-solved-break-apart-strings/#findComment-588134 Share on other sites More sharing options...
DarkWater Posted July 12, 2008 Share Posted July 12, 2008 Oh. And if you ever need a SIMPLE replace, use str_replace instead of preg_replace or ereg_replace. And use strpos() instead of a regex to check to see if something is present, if possible. Link to comment https://forums.phpfreaks.com/topic/114366-solved-break-apart-strings/#findComment-588135 Share on other sites More sharing options...
proggR Posted July 12, 2008 Author Share Posted July 12, 2008 Thank you both. I just jotted them all down and I'll take a look more into them when I get a chance. Thanks again. Link to comment https://forums.phpfreaks.com/topic/114366-solved-break-apart-strings/#findComment-588179 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.