Asked  7 Months ago    Answers:  5   Viewed   48 times

I want to display numbers as follows

  • 1 as 1st,
  • 2 as 2nd,
  • ...,
  • 150 as 150th.

How should I find the correct ordinal suffix (st, nd, rd or th) for each number in my code?

 Answers

88

from wikipedia:

$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if (($number %100) >= 11 && ($number%100) <= 13)
   $abbreviation = $number. 'th';
else
   $abbreviation = $number. $ends[$number % 10];

Where $number is the number you want to write. Works with any natural number.

As a function:

function ordinal($number) {
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');
    if ((($number % 100) >= 11) && (($number%100) <= 13))
        return $number. 'th';
    else
        return $number. $ends[$number % 10];
}
//Example Usage
echo ordinal(100);
Wednesday, March 31, 2021
 
peixotorms
answered 7 Months ago
72

This seems to be a JPEG-File, so you should be able to send that data together with the appropriate mime-type to the browser. It should be possible to output that image with something like:

<img src="data:image/jpeg;base64,<?php echo base64_encode($imageString); ?>"/>

But it might also be possible to save files of any image format into that thumbnailPhoto attribute. Therefore, I would put the content into a temporary file that will then be served directly from the server. You will need to pass the file through finfo to get the correct mime-type.

So you might do something like this:

$tempFile = tempnam(sys_get_temp_dir(), 'image');
file_put_contents($tempFile, $imageString);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime  = explode(';', $finfo->file($tempFile));
echo '<img src="data:' . $mime[0] . ';base64,' . base64_encode($imageString) . '"/>';
Wednesday, March 31, 2021
 
msg
answered 7 Months ago
msg
11

Maybe:

$num = 245354;
$numlength = strlen((string)$num);
Wednesday, March 31, 2021
 
JackTheKnife
answered 7 Months ago
100

Use number_format function. With space for thoudsandsSparator as below.

number_format($number, 0, '.', ' ')

Codepad Demo.

Wednesday, March 31, 2021
 
rojo
answered 7 Months ago
99

The best way would be to reformat the array so the keys were the directories, and the array values were arrays containing file names, like so:

$array = array( ..);
$reformatted = array();
foreach( $array as $k => $v) {
    list( $key, $value) = explode( '|', $v);
    if( !isset( $reformatted[$key])) 
        $reformatted[$key] = array();
    $reformatted[$key][] = $value;
}

Then you just have to iterate over the new array, like so:

foreach( $reformatted as $dir => $files) {
    echo $dir . "n";
    foreach( $files as $file)
        echo "t" . $file . "n";
}

This outputs:

dir0
    file0.txt
    file1.txt
dir1
    file2.txt
    filea.txt
dir2
    fileb.txt

Note that this will only work for plain text environment (such as <pre></pre>, like in the demo). Otherwise, you'll need to use <br /> instead of n for line breaks, or use an ordered or unordered list.

For HTML output, use this, whose output can be seen here

echo '<ul>';
foreach( $reformatted as $dir => $files) {
    echo "<li>$dir</li>";
    echo '<ul>';
    foreach( $files as $file)
        echo "<li>$file</li>";
    echo '</ul>';
}
echo '</ul>';

Generates:

  • dir0
    • file0.txt
    • file1.txt
  • dir1
    • file2.txt
    • filea.txt
  • dir2
    • fileb.txt

For your updated array, here is the solution:

$reformatted = array(); $weights = array();
foreach( $paths as $k => $v) {
    list( $key, $value) = explode( '|', $v[0]);

    if( !isset( $reformatted[$key])) 
        $reformatted[$key] = array();

    if( !isset( $weights[$key]))
        $weights[$key] = 0;

    $reformatted[$key][] = array( $value, $v[1]);
    $weights[$key] += $v[1];    
}

foreach( $reformatted as $dir => $files) {
    echo $dir . ' (' . $weights[$dir] . ")n";
    foreach( $files as $file)
        echo "t" . $file[0] . ' (' . $file[1] . ")n";
}

This outputs:

dir0 (900)
    file0.txt (400)
    filea.txt (500)
dir1 (1300)
    file1.txt (600)
    fileb.txt (700)
dir2 (700)
    filec.txt (700)

I'll leave it up to you to translate that into HTML if necessary.

Saturday, May 29, 2021
 
employeegts
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :