Skip to main content
Participant
September 22, 2010
Answered

PHP convert text function

  • September 22, 2010
  • 3 replies
  • 937 views

Hi there im wondering if you can help me out.

I’m basically trying to write a function where a number will be replaced by text… but im rubbish.

Heres my situation.

- im drawing my content from a database, so it will be displayed as this:

     <?php echo $row_psanews[‘psanews_datem’]; ?>

      as an example the page will result in echoing: 10

I want to write a function which will convert this into text.

So I’ll change my php into:

     <?php echo converter ($row_psanews[‘psanews_datem’]); ?>

     converter being the name of the function, the page will result in echoing: October

.. the function would be something along the lines of

function converter (){

01 = “January”

02= "February"

etc etc

its all a major learning curb for me, and I hope im making sense here

This topic has been closed for replies.
Correct answer geotrice

It seems like you're trying to do two different things:

  1. Convert a number to a custom string of text.
  2. Convert a number (1-12) to the textual representation of that month.

If 1 is what you want, then a switch is probably the simplest and best route to take.

switch($row_psanews['psanews_datem']){

     case 1:

          //code to be performed

          //maybe something like

          $result = "January";

          break; //Break out of this case.

     case 2:

          //code to be performed

          $result = "February";

          break; //Break out of this case.

     //add more cases here...

     //don't forget a default case!

     default:

          //code to be performed if we couldn't account for a possibility

          $result = "Unknown";

          //No break needed, since this is the last check.

}

Check out this page on the Switch Statement.

If 2 is what you need, then PHP has a built in function for that as part of it's Date Function.

<?php

//row_psanews is a whole number (1-12)

$row_psanews[‘psanews_datem’]; //10, in this example

//Create a timestamp

//mktime(hour, minute, second, month, day, year, daylight savings time)

$month = mktime(0,0,0,$row_psanews['psanews_datem'],0,0,0);

//Find the month

$month_name = date("F", $month);

//Write out the month name

echo($month_name);

//Writes out "October"

?>

For formatting the result of the date function check this out.


For other date functions check this out.

3 replies

mannouchAuthor
Participant
September 23, 2010

I love all of you guys!

Thank you so much for your help!

Each technique is exactly what Im looking for.

I had discovered preg-replace before this post, but i couldnt work it out, but managed to get one working a few hours later. whish i could tag everyone as giving the correct answer.

Many thanks again for your time! Life savers

geotrice
geotriceCorrect answer
Inspiring
September 23, 2010

It seems like you're trying to do two different things:

  1. Convert a number to a custom string of text.
  2. Convert a number (1-12) to the textual representation of that month.

If 1 is what you want, then a switch is probably the simplest and best route to take.

switch($row_psanews['psanews_datem']){

     case 1:

          //code to be performed

          //maybe something like

          $result = "January";

          break; //Break out of this case.

     case 2:

          //code to be performed

          $result = "February";

          break; //Break out of this case.

     //add more cases here...

     //don't forget a default case!

     default:

          //code to be performed if we couldn't account for a possibility

          $result = "Unknown";

          //No break needed, since this is the last check.

}

Check out this page on the Switch Statement.

If 2 is what you need, then PHP has a built in function for that as part of it's Date Function.

<?php

//row_psanews is a whole number (1-12)

$row_psanews[‘psanews_datem’]; //10, in this example

//Create a timestamp

//mktime(hour, minute, second, month, day, year, daylight savings time)

$month = mktime(0,0,0,$row_psanews['psanews_datem'],0,0,0);

//Find the month

$month_name = date("F", $month);

//Write out the month name

echo($month_name);

//Writes out "October"

?>

For formatting the result of the date function check this out.


For other date functions check this out.

September 22, 2010

http://php.net/manual/en/function.preg-replace.php

(found by searching for php replace. remember... search is your friend!!!)

September 22, 2010

It might be a little more intuitive to use arrays. Regex can be a bear at the best of times.

Say you set the variable $value = 10; Define an array like:

$months[1] = "January";

$months[2] = "February";

...

then your output would be

     echo ($months[$value]);

Barry