php.ajax.coder Posted March 11, 2009 Share Posted March 11, 2009 I have a string like below "hello it's 19:10" I need to split by white space leave : if its next to two numbers e.g. times otherwise remove same for / for dates Is there an easy way of doing this Thanks Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 Do you mean explode to make a array, then work with the data. not tested. <?php $word="hello it's 19:10"; $a=explode(' ',$word); echo"<pre>"; print_r($a); echo"</pre>"; ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 result tested. <pre>Array ( [0] => hello [1] => it's [2] => 19:10 ) </pre> Quote Link to comment Share on other sites More sharing options...
php.ajax.coder Posted March 11, 2009 Author Share Posted March 11, 2009 This works for this string but i need to remove ":" from things like "Hello: welcome" Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 <?php $word="hello: it's redarrow:"; $a=str_replace(':','',$word); $b=explode(' ',$a); echo"<pre>"; print_r($b); echo"</pre>"; ?> result. <pre>Array ( [0] => hello [1] => it's [2] => redarrow ) </pre> Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 yes but that doesnt satisfy his other condition leave : if its next to two numbers e.g. times otherwise remove same for / for dates Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 geek way lol. <?php $word="hello: it's redarrow:"; $r=str_replace(':','',$word); $a=explode(' ',$r); $b=array_chunk($a,1); echo"<pre>"; print_r($b); echo"</pre>"; ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 12, 2009 Share Posted March 12, 2009 what wrong with this mate it works. <?php $word="hello it's redarrow"; if(strstr($word,':')){ $r=str_replace(':','',$word); $a=explode(' ',$r); print_r($a); }else{ $a=explode(' ',$word); print_r($a); } ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 12, 2009 Share Posted March 12, 2009 try this <?php $word="Hello: welcome / it's 19:10 12/03/2009"; $word = preg_replace('%(?<=[^\d])(:|/)(?=[^\d])%sm', '', $word); $NewWord = preg_split('/\s+/sm', $word); print_r($NewWord); ?> EDIT: oops forgot about the date! Added Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 12, 2009 Share Posted March 12, 2009 love the regexp explain it please, bit by bit well good. i am learning advance regexp love that one. this group well good like to no what it means cheers. (?<=[^\d]) <?php $word="Hello: welcome it's ".date("d/m/y,h:i:s").""; $word = preg_replace('/(?<=[^\d])((?=[^\d])/sm', '', $word); $NewWord=explode(' ',$word); print_r($NewWord); ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 12, 2009 Share Posted March 12, 2009 its a positive lookbehind that matchs any character that is NOT a A single digit. 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.