PHP Classes

File: example.php

Recommend this page to a friend!
  Packages of Artur Graniszewski   Java compatible UTF-8 Data Manipulation   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Java compatible UTF-8 Data Manipulation
Manipulate text strings as in Java
Author: By
Last change: Addes some examples based on Java SDK.
Date: 16 years ago
Size: 3,562 bytes
 

 

Contents

Class file image Download
<?php

header("Content-Type: text/html; charset=utf-8");

require("./java_datatypes.php");

$locale = new Locale(Locale::POLISH);

$s1 = new String("wołowina");
$s2 = new String("wól");
$s3 = new String("wół");
$s4 = new String("wołowina");


echo "<pre><strong>String comparison:</strong><br />";
echo ($s1." ".($s1->compareTo($s2) > 0 ? 'is after' : 'is before')." ".$s2);
echo "<br />";

echo ($s2." ".($s2->compareTo($s3) > 0 ? 'is after' : 'is before')." ".$s3);
echo "<br />";

echo ($s3." ".($s3->compareTo($s1) > 0 ? 'is after' : 'is before')." ".$s1);
echo "<br />";

echo $s4->toUpperCase().' is '.($s4->toUpperCase()->compareTo($s1->toUpperCase()) == 0 ? 'equal' : 'not equal').' to '.$s1->toUpperCase();
echo "<br />";

echo $s4.' is '.($s4->compareToIgnoreCase($s1->toUpperCase()) == 0 ? 'equal' : 'not equal').' to '.$s1->toUpperCase().' (compareToIgnoreCase)';
//-----------------------------------------------------------
echo "<hr/>";
echo "<strong>String validation:</strong><br />";

$s5 = chr(0x81)."łąka";
$valid = String::isValid($s5);
echo "$s5 is a".($valid === 0 ? 'n invalid UTF-8' : ($valid === 1 ? ' pure ASCII (valid UTF-8)' : ' valid UTF-8'))." string";
echo "<br />";

$s5 = "łąka";
$valid = String::isValid($s5);
echo "$s5 is a".($valid === 0 ? 'n valid UTF-8' : ($valid === 1 ? ' pure ASCII (valid UTF-8)' : ' valid UTF-8'))." string";
echo "<br />";

$s5 = "laka";
$valid = String::isValid($s5);
echo "$s5 is a".($valid === 0 ? 'n valid UTF-8' : ($valid === 1 ? ' pure ASCII (valid UTF-8)' : ' valid UTF-8'))." string";

//-----------------------------------------------------------
echo "<hr/>";
echo "<strong>String char-by-char iteration:</strong><br />";

echo "char-by-char 'for' loop in reverse order: <br />&nbsp;&nbsp;";
for($i = $s1->length()-1; $i >= 0; $i--) {
    echo $s1[$i];
}

echo " (original message $s1)";
echo "<br />";

echo "char-by-char 'for' loop in reverse order with mixed case: <br />&nbsp;&nbsp;";
for($i = $s1->length()-1; $i >= 0; $i--) {
    if($i%2) {
        echo $s1[$i];
    } else {
        echo $s1[$i]->toUpperCase();
    }
}

echo " (original message $s1)";
echo "<br />";

//-----------------------------------------------------------
echo "<hr/>";
echo "<strong>String manipulation:</strong><br />";

echo "length of $s1 is ".$s1->length();
echo "<br />";

echo "the third char of $s1 is ".$s1->charAt(2);
echo "<br />";

echo "the sixth char of $s1 is ".$s1->charAt(5);
echo "<br />";

echo "$s1 padded right to 10 chars (not bytes!) is ".$s1->padRight(10, "_");
echo "<br />";

echo "$s1 uppercased is ".$s1->toUpperCase();
echo "<br />"; 

echo "$s1 replaced ('w' => 'W') is ".$s1->replaceAll('w', 'W');
echo "<br />";

echo "$s1 replaced ('[n-z]' => '*') is ".$s1->replaceAll('[n-z]', '*');
echo "<br />";

echo "$s1 starts with 'woł'? ". ($s1->startsWith('woł') ? 'true' : 'false');
echo "<br />";

echo "$s1 starts at index 3 with 'owi'? ". ($s1->startsWith('owi', 3) ? 'true' : 'false');
echo "<br />";

echo "$s1 ends with 'ina'? ". ($s1->endsWith('ina') ? 'true' : 'false');
echo "<br />";

echo "the index of 'i' character is equal ".$s1->indexOf('i');
//-----------------------------------------------------------
echo "<hr/>";
echo "<strong>String matching (java patterns):</string><br />";
$p = Pattern::compile("a*b");
$m = $p->matcher("aaaaab");
$b = $m->matches();
echo "pattern ".$p->pattern()." matching result: ".(int) $b;
echo "</pre>";

?>