PHP Classes

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: 2 months ago
Size: 6,029 bytes


Class file image Download
<?php echo '<!DOCTYPE html>' ?>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Testing of the "sci" Class</title>
 * 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 (

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

//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) {
'"',(string) $test,'" : ';

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


foreach (
$array as $test) {
'"',(string) $test,'" : ';

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

'Example taking the HTML output (view the source code and note that the choosen LOP was si_short_html)'.PHP_EOL;

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.'
try {
var_dump($sci1->get_number('20µµ')); //20micromicro
} catch (Exception $e) {
'Error Found: ',$e->getMessage(),PHP_EOL;

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');

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 '

PHP_EOL.'Example becoming independent the number and prefix:'.PHP_EOL;
'<em>"The last voltage measuring was of '.$result[0].' <strong style="color: red;">'.$result[1].'V</strong>"</em>

//"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):'
$examples as $key => $example) {
'"'.$example.'" : ';
    try {
'$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){
'Error Found (',$e->getCode(),'): ',$e->getMessage(),PHP_EOL;
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.'

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.

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) {
'Error Found: ',$e->getMessage(),PHP_EOL;
echo 'Repeat until it stops giving exceptions and returns a number.</pre></body></html>';

For more information send a message to info at phpclasses dot org.