Jump to content


exploding a string


  • Please log in to reply
3 replies to this topic

#1 Guest_edwinsweep_*

Guest_edwinsweep_*
  • Guests

Posted 20 June 2006 - 01:00 PM

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?

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 June 2006 - 01:03 PM

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:
<?php
$string = "21";
preg_match('|[0-9]|', $string, $match);
foreach ($match as $digit) echo "$digit<br />\n";
?>

or, if you simply need to know the last character of the string, try this:
<?php
$string = "21";
echo substr($string, -1);
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Guest_edwinsweep_*

Guest_edwinsweep_*
  • Guests

Posted 20 June 2006 - 01:15 PM

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.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 June 2006 - 01:52 PM

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:
<?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().
?>

Ken





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users