PHP Classes
elePHPant
Icontem

File: examples.php

Recommend this page to a friend!
  Classes of Alex F. Bustos  >  SI Prefix Converter  >  examples.php  >  Download  
File: examples.php
Role: Example script
Content type: text/plain
Description: Many possible uses of the class
Class: SI Prefix Converter
Convert a number to a string appending a prefix
Author: By
Last change: 1.2 Version. Changed, backward compatible with 1.1. Corrected and added short sets and spanish long version of the prefixes.
Date: 3 years ago
Size: 6,029 bytes
 

 

Contents

Class file image Download
<?php echo '<!DOCTYPE html>' ?>
<html><head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Testing of the "sci" Class</title>
</head>
<body>
<pre><?php
/**
 * File: examples.php
 * Author: Alex F. Bustos - alexfbp at gmail dot com
 *
 * Non required file. Feel free to delete/hide/block...
 * I suggest you first to open the browser output of this.
 * Then, if you have curiosity; open again this file.
 */
include('settings.inc.php');
include (
'sci.class.php');

echo
'Default settings (ENG mode, 3 significant digits, base 10) and HTML List of Prefixes (LOP)'.PHP_EOL;
$sci1=new sci('si_short_html');
$sci1->start();

$array=array(
   
//number - scale
   
' -5486800', //1M
   
54860, //100K
   
'-54868', //10K
   
'+5486.8', //1K
   
-548.68, //100
   
'+54.868', //10
   
' -5.4868 ', //1
   
'+.54868', //100m
   
'-.054868', //10m
   
'+.0054868', //1m
   
' -.00054868 ', //100u
   
'+.000054868', //10u
   
' +.000054868' //1u
   
);

foreach (
$array as $test) {
    echo
'"',(string) $test,'" : ';
   
var_dump($sci1->get_prefixed($test));
}

echo
PHP_EOL.'Same Example, but in decimal mode (no significant digits)'.PHP_EOL;

$sci1->set_round_params(2,false);

foreach (
$array as $test) {
    echo
'"',(string) $test,'" : ';
   
var_dump($sci1->get_prefixed($test));
}

echo
PHP_EOL.'<u>Output Recognition</u> - Maybe you first should choose the right charset through
the set_charset() function or modifying the settings file.'
.PHP_EOL;

echo
'Example taking the HTML output (view the source code and note that the choosen LOP was si_short_html)'.PHP_EOL;
var_dump($sci1->get_number($sci1->get_prefixed(0.0002579),false));
var_dump($sci1->get_number($sci1->get_prefixed(5937),false));

echo
PHP_EOL.'Example with special character in the string. If you changed the encoding of this
file (perhaps simply by the fact of having edited), maybe this example won\'t work.'
.PHP_EOL;
try {
   
var_dump($sci1->get_number('20µµ')); //20micromicro
} catch (Exception $e) {
    echo
'Error Found: ',$e->getMessage(),PHP_EOL;
}

echo
PHP_EOL.'<u>Example with base change:</u>'.PHP_EOL;
//$sci2=new sci('binary_short',2,0,40); //old example
//$sci2=new sci('binary_short',null,null,null); //new, but equivalent to:
$sci2=new sci('binary_short');
$sci2->set_scale_rule(10);
$sci2->start();

$size=4294967296;
echo
PHP_EOL.'"Usually in MS Windows, a drive with '.$size.'B would be informed as an unit of '.$sci1->get_prefixed($size).'B,
but when you look in some Linux distros, it will appear simply as an unit of '
.$sci2->get_prefixed($size).'B."'.PHP_EOL;

echo
PHP_EOL.'Example becoming independent the number and prefix:'.PHP_EOL;
$result=$sci1->get_prefixed(4000,true);
echo
'<em>"The last voltage measuring was of '.$result[0].' <strong style="color: red;">'.$result[1].'V</strong>"</em>
'
;

$examples=array(
   
//"numeric part" - "prefix part" -> result
   
'1.2Mp', //"1.2" - "Mp" -> ok
   
'1 kk', //"1" - " kk" -> ok
   
'-1µ µ', //"-1" - "µ µ" -> ok if charset ok.
   
'2e3km', //"2e3" - "km" ->ok
   
'4E5 m', //"4E5" - " m" ->ok
   
'2 E k', //"2" -> "Ek" -> Error->Exception. The E are not a prefix.
   
'k'); //Can also be used the prefix alone

echo PHP_EOL.'<u>Processing of Multiple Prefixes</u>
Suppose that you have a field in wich you\'re asking for a capacitor value in
picofarads (pF), and someone wants to use a capacitor of 1&micro;F. That are,
also, 1\'000.000 of pF. But, why introduce that if you could use simply the
"1M" value?

Other similar example are when you are asking a resistor value in K&Omega; and
someone wants to use a 1M&Omega; resistor, that are an 1000K&Omega; resistor.
Again, why to insert the 1000 when you can insert 1K?

That class allows the simplification of prefixes, knowing that a prefix are
an abbreviation of a multiplication by another number.

There are both and additional samples (intentional error in the penultimate string - see the source code):'
.PHP_EOL;
foreach(
$examples as $key => $example) {
    echo
'"'.$example.'" : ';
    try {
        echo
           
'$sci->get_number($examples['.$key.']) = $sci->get_number('.htmlentities($example,ENT_COMPAT,'UTF-8').') = ',
            (
$aux=$sci1->get_number($example)).' = '.$sci1->get_prefixed($aux).PHP_EOL;
    } catch (
Exception $e){
        echo
'Error Found (',$e->getCode(),'): ',$e->getMessage(),PHP_EOL;
    }
}
echo
'
Note that in $examples[3] the string was '
.$examples[3].', which has a float number (2e3).
It means that if you don\'t want to write 2000 as 2K, but 2e3 or 2E3; you can do it also.
That behaviour are system dependant (string to -numeric format, probably float- conversion)

What happened with $examples[5]?
Supoose that you have a LOP conformed by "foo", "e" and other prefixes.
If you insert a number, as example "2e3foo" the class will recognize the numeric part as "2e3"
and the prefixed part as "foo" other example, "2efoo" will recognize the numeric part as "2"
and the prefix part as "efoo". This because the prefix part are recognized analyzing the string
from right to left and, note that the prefix part are case-sensitive.'
.PHP_EOL;

echo
PHP_EOL. '<u>SUGGESTED "CALIBRATION" PROCESS FOR THE ENCODING</u>
The string that you could be viewing as the assigned value for $examples[2] should
be something like "-1u u", where each \'u\' are, or the micro or the mu character.

Don\'t forget to set up the encoding format, in both parts:
->In the head of the HTML document; through the meta tag:'
.PHP_EOL.' '.
htmlentities('<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />').
'
->In the configuration of the class, through the __construct() or set_charset() functions.

STEPS:
1. Copy the output of your browser of one of this characters: micro (&micro;) or mu (&mu;)
2. Paste as the argument of the function inside the next "try-catch" block
   (edit the source code to see it):
'
;
try {
   
var_dump($sci1->get_number('1µµ')); //2. Change each of the 'u's by the copied character
} catch (Exception $e) {
    echo
'Error Found: ',$e->getMessage(),PHP_EOL;
}
//
echo 'Repeat until it stops giving exceptions and returns a number.</pre></body></html>';
exit;