Perad Posted August 19, 2007 Share Posted August 19, 2007 I think i might need to use preg_replace. However I have never used it before and can't workout how to find the following. This is my string.. Call Out Fee for 10:00 on 20/8/2007 What I need is for this string to be reduced to 10:00 and 20/8/2007. These values would preferably be in an array. Could someone give me a hand with this please. Quote Link to comment https://forums.phpfreaks.com/topic/65672-finding-sub-strings/ Share on other sites More sharing options...
php_dave Posted August 19, 2007 Share Posted August 19, 2007 Hi, is the string size and position constant? If so I would do something like: $string = "Call Out Fee for 10:00 on 20/8/2007"; array[1] = substr($string, 17, 5); array[2] = substr($string, 26, 9); As i say this will only work if the string is consistant in length and there is probably a better way Cheers Dave Quote Link to comment https://forums.phpfreaks.com/topic/65672-finding-sub-strings/#findComment-327954 Share on other sites More sharing options...
Perad Posted August 19, 2007 Author Share Posted August 19, 2007 This 20/8/2007 could become 20/10/2007. The date can increase and decrease by 1 integer Quote Link to comment https://forums.phpfreaks.com/topic/65672-finding-sub-strings/#findComment-327955 Share on other sites More sharing options...
sasa Posted August 19, 2007 Share Posted August 19, 2007 try <?php $string = "Call Out Fee for 10:00 on 20/8/2007"; preg_match_all('/([0-9]+:[0-9]+)[^0-9]*([0-9]+\/[0-9]+\/[0-9]+)/', $string, $result); print_r($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65672-finding-sub-strings/#findComment-327964 Share on other sites More sharing options...
JasonLewis Posted August 19, 2007 Share Posted August 19, 2007 how about this? it works for me... <?php $string = "Call Out Fee for 10:00 on 20/8/2007"; $out_string = array_values(array_filter(explode(" ", ereg_replace("[[:alpha:]]", "", $string)))); print_r($out_string); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65672-finding-sub-strings/#findComment-327977 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 sasa would be faster than ProjectFear as were all doing one heres mine <?php $data = "Call Out Fee for 10:00 on 20/8/2007"; if (preg_match('%(\d+:\d+)[\D]*([\d/]{8,10})%si', $data, $regs)) { echo $regs[0]; echo "<br />time is "; echo $regs[1]; echo " date is "; echo $regs[2]; } ?> NOTE: $regs[0] is the full string $regs[1] is the time $regs[2] is the date Quote Link to comment https://forums.phpfreaks.com/topic/65672-finding-sub-strings/#findComment-327983 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.