galayman Posted November 30, 2007 Share Posted November 30, 2007 Hi All, i have this array $ary = array( "Today date is 1, our breakfast is 2 eggs and salad", "Today date is 13, our breakfast is 3 breads and beff", "Today date is 18, our breakfast is 2 toasts with egg", "Today date is 21, our breakfast is 5 wafels with honey", "Today date is 30, our breakfast is a bowl of sereal" ); i need to search and match the date (that i have bolded it) and then compare it to today date using this fuction date("j"); to find the date in the string i'm using this regex ([1-3][0-9]?)(?=, our) i have try everything but i cannot solve it so in the end, if today date is 18 i want to display only this string Today date is 18, our breakfast is 2 toasts with egg can i do this? Thank You , Best Regards Link to comment https://forums.phpfreaks.com/topic/79601-solved-match-an-array-with-a-date-and-print-the-result/ Share on other sites More sharing options...
Stooney Posted November 30, 2007 Share Posted November 30, 2007 try using the stristr() function to search for 'is $today' where $today is the result of date("j"). I use the 'is' so you dont get results pertaining to the number of eggs and whatnot. Link to comment https://forums.phpfreaks.com/topic/79601-solved-match-an-array-with-a-date-and-print-the-result/#findComment-403169 Share on other sites More sharing options...
marcus Posted November 30, 2007 Share Posted November 30, 2007 You can try this. <?php $ary = array( "Today date is 1, our breakfast is 2 eggs and salad", "Today date is 13, our breakfast is 3 breads and beff", "Today date is 18, our breakfast is 2 toasts with egg", "Today date is 21, our breakfast is 5 wafels with honey", "Today date is 30, our breakfast is a bowl of sereal" ); $date = date("j"); foreach($ary AS $ara){ $ex = explode(" ", $ara); $ex = str_replace(",", NULL, $ex); if($ex[3] == $date){ $what = $ara; } } echo $what; ?> tested to some extent. Link to comment https://forums.phpfreaks.com/topic/79601-solved-match-an-array-with-a-date-and-print-the-result/#findComment-403172 Share on other sites More sharing options...
kenrbnsn Posted November 30, 2007 Share Posted November 30, 2007 Can you change your array? If you can change it to: <?php $ary = array(1 => '2 eggs and salad', 13 => '3 breads and beff', 18 => '2 toasts with egg', 21 => '5 wafels with honey', 31 => 'a bowl of cereal'); ?> The the solution is very easy. <?php $date = date('j'); echo 'Today is ' . $date. ', our breakfast is ' . $ary($j); ?> Ken Link to comment https://forums.phpfreaks.com/topic/79601-solved-match-an-array-with-a-date-and-print-the-result/#findComment-403183 Share on other sites More sharing options...
galayman Posted November 30, 2007 Author Share Posted November 30, 2007 waw! phpfreaks guru is the best! Thank you, i love php and i love everyone here too! Link to comment https://forums.phpfreaks.com/topic/79601-solved-match-an-array-with-a-date-and-print-the-result/#findComment-403186 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.