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 Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/ 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/#findComment-782540 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> Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/#findComment-782541 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" Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/#findComment-782553 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> Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/#findComment-782556 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 Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/#findComment-782559 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/#findComment-782564 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); } ?> Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/#findComment-782578 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 Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/#findComment-782585 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); ?> Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/#findComment-782597 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. Link to comment https://forums.phpfreaks.com/topic/149021-solved-preg_split/#findComment-782602 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.