Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

PHP convert text function

New Here ,
Sep 22, 2010 Sep 22, 2010

Copy link to clipboard

Copied

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

TOPICS
Server side applications

Views

855
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 22, 2010 Sep 22, 2010

Copy link to clipboard

Copied

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

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 22, 2010 Sep 22, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 22, 2010 Sep 22, 2010

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 23, 2010 Sep 23, 2010

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines