Guest edwinsweep Posted June 20, 2006 Share Posted June 20, 2006 hi everybody, im trying to figure out how to explode or split a string that has only 2 integer values, without a seperator.actualy its the day nr of the month.when its 21 i want it to say 21st and when its 25 it has to say 25th.so first i need to split the 2 numbers to figure out what the last one is!there probely is another way to figure this out but i would like to know how to split that damn string.it has no seperator so how would this be done? Quote Link to comment https://forums.phpfreaks.com/topic/12464-exploding-a-string/ Share on other sites More sharing options...
obsidian Posted June 20, 2006 Share Posted June 20, 2006 there are a couple different ways. if you simply want to split the string into it's separate integers no matter how many there are, you may want to use something like this:[code]<?php$string = "21";preg_match('|[0-9]|', $string, $match);foreach ($match as $digit) echo "$digit<br />\n";?>[/code]or, if you simply need to know the last character of the string, try this:[code]<?php$string = "21";echo substr($string, -1);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12464-exploding-a-string/#findComment-47643 Share on other sites More sharing options...
Guest edwinsweep Posted June 20, 2006 Share Posted June 20, 2006 Yep that's it, that's what i whas looking for.thanks you very much, you saved me lot's of time.it so easy isn't it?it just that you have to know it to use it. Quote Link to comment https://forums.phpfreaks.com/topic/12464-exploding-a-string/#findComment-47647 Share on other sites More sharing options...
kenrbnsn Posted June 20, 2006 Share Posted June 20, 2006 You indicated that the string contains the day of a month, if that is the case, you can use a combination of the strotime() function and the date() function:[code]<?phpecho 'Today is the ' . date('jS') . ' of the month of ' . date('F') . ' in the year ' . date('Y') . '<br>';$rand_month = rand(1,12);$max_days = date('t',strtotime(date('Y') . '-' . $rand_month . '-01'));$rand_day = rand(1,$max_days);$rand_year = rand(date('Y'),2037); // the date function might break in 2038!$rand_date = strtotime($rand_year . '-' . $rand_month . '-' . $rand_day);echo 'In the year ' . $rand_year . ', the ' . date('jS',$rand_date) . ' of ' . date('F',$rand_date) . ' falls on a ' . date('l',$rand_date) . '<br>'; // that's a lowercase 'L' in the last date().?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/12464-exploding-a-string/#findComment-47662 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.