Jump to content

Archived

This topic is now archived and is closed to further replies.

Guest edwinsweep

exploding a string

Recommended Posts

Guest edwinsweep
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest edwinsweep
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.

Share this post


Link to post
Share on other sites
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]<?php
echo '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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.