Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/12464-exploding-a-string/
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]
Link to comment
https://forums.phpfreaks.com/topic/12464-exploding-a-string/#findComment-47643
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
Link to comment
https://forums.phpfreaks.com/topic/12464-exploding-a-string/#findComment-47662
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.