EchoFool Posted January 3, 2010 Share Posted January 3, 2010 Hey Im trying to use a str_explode but on the basis that the words are "between" the symbol.... heres what i mean: Say i got: #username1#usrname2# becomes username1 and username2 username1#username2# is username2 only. and username1#username2 is nothing. I want to seperate with : print_r(explode('#', $str, 2)); But specifically with the picky method involved, how do i edit the explode to do it ? Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/ Share on other sites More sharing options...
sasa Posted January 3, 2010 Share Posted January 3, 2010 try <?php $test = '#username1#usrname2'; $out = explode('#', $test); array_shift($out); array_pop($out); print_r($out); ?> Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-987738 Share on other sites More sharing options...
EchoFool Posted January 3, 2010 Author Share Posted January 3, 2010 Works perfect - thanks! Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-987759 Share on other sites More sharing options...
EchoFool Posted January 3, 2010 Author Share Posted January 3, 2010 Actually no it doesn't work! I tried: #username#username2# And array shows: Array ( => [1] => username [2] => username2 [3] => ) Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-987768 Share on other sites More sharing options...
gizmola Posted January 3, 2010 Share Posted January 3, 2010 Why make this so complicated. Just trim off the start and end "#". print_r(explode('#', trim($str, '#')); Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-987784 Share on other sites More sharing options...
EchoFool Posted January 3, 2010 Author Share Posted January 3, 2010 Tried that with: $text = '#username#usrname2# wasup guys !'; $out = explode('#', trim($text, '#')); $string = $out[0]; Echo $string; But that doesn't seem to work. Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-987796 Share on other sites More sharing options...
laffin Posted January 3, 2010 Share Posted January 3, 2010 You must be doing something wrong, cuz i get the right output <?php function delim_explode($delim,$string) { if(in_array($delim,array('/','\\','[',']','(',')'))) $delim="\\{$delim}"; $pattern="((?<=$delim).*?(?=$delim))+"; if(preg_match_all("/{$pattern}/",$string,$matches)) { $matches=$matches[1]; } else $matches=array(); return $matches; } function sasa_explode($delim,$string) { $out = explode($delim, $string); array_shift($out); array_pop($out); return($out); } header('Content-Type: text/plain'); var_dump(delim_explode('#','#username1#username2#')); var_dump(delim_explode('#','username1#usrname2#')); var_dump(delim_explode('#','#username#usrname2# wasup guys !')); var_dump(delim_explode('#','no delimeter')); var_dump(sasa_explode('#','#username1#username2#')); var_dump(sasa_explode('#','username1#usrname2#')); var_dump(sasa_explode('#','#username#usrname2# wasup guys !')); var_dump(sasa_explode('#','no delimeter')); ?> I do like sasa's method, simple Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-987812 Share on other sites More sharing options...
salathe Posted January 4, 2010 Share Posted January 4, 2010 How about the following? $str = 'abc#username#username2#foo'; // Here's the magical voodoo!.. $split = array_slice(explode('#', $str), 1, -1); print_r($split); /* Array ( [0] => username [1] => username2 ) */ Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-987873 Share on other sites More sharing options...
gizmola Posted January 4, 2010 Share Posted January 4, 2010 Tried that with: $text = '#username#usrname2# wasup guys !'; $out = explode('#', trim($text, '#')); $string = $out[0]; Echo $string; But that doesn't seem to work. You are missing something somewhere. My simple code does indeed work. Full script here: $text = '#username#username2# wasup guys!'; $string = explode ('#', trim($text, '#')); print_r($string); Output using php -f [david@penny ~]$ php -f explode.php Array ( [0] => username [1] => username2 [2] => wasup guys! ) Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-987910 Share on other sites More sharing options...
salathe Posted January 4, 2010 Share Posted January 4, 2010 gizmola, given the text from your last post, EchoFool doesn't want " wasup guys!" to be one of the items since it is not surrounded by # characters. Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-988113 Share on other sites More sharing options...
laffin Posted January 5, 2010 Share Posted January 5, 2010 Nice code salathe, I think for simplicity yours wins hands down. But its nice to see the various routines you can use to perform such a task Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-988630 Share on other sites More sharing options...
gizmola Posted January 5, 2010 Share Posted January 5, 2010 gizmola, given the text from your last post, EchoFool doesn't want " wasup guys!" to be one of the items since it is not surrounded by # characters. That's what I get for not reading the OP carefully enough. Thanks Salathe... you are right, I missed that nuance entirely. Link to comment https://forums.phpfreaks.com/topic/187040-str-explode-between-symbols/#findComment-988695 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.