Skip to content

Instantly share code, notes, and snippets.

@hightemp
Last active August 19, 2018 13:37
Show Gist options
  • Select an option

  • Save hightemp/bfb475d8ecc244a4ebb6a5cb587088a9 to your computer and use it in GitHub Desktop.

Select an option

Save hightemp/bfb475d8ecc244a4ebb6a5cb587088a9 to your computer and use it in GitHub Desktop.
Test of str_replace and preg_replace functions
<?php
// file encoding utf-8
echo "encoding utf-8\n\n";
//$a = "\xe1\x84\xbf\x84"; //ᄿ?
$a = "ᄿtest"; //e1 84 bf 74 65 73 74
echo "string ".$a."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $a)))."\n\n";
$c = "\x84";
echo "replace char ".$c."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $c)))."\n\n";
echo "str_replace ".($r = str_replace("$c", '', $a))."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $r)))."\n\n";
echo "preg_replace ".($r = preg_replace("/".preg_quote($c)."/", '', $a))."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $r)))."\n\n";
echo "preg_replace with unicode ".($r = preg_replace("/".preg_quote($c)."/u", '', $a))."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $r)))."\n\n";
$u = utf8_encode($c);
echo "replace char unicode ".$u."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $u)))."\n\n";
echo "preg_replace with unicode ".($r = preg_replace("/".preg_quote($u)."/u", '', $a))."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $r)))."\n\n";
$c = "\x84\xbf";
echo "replace char ".$c."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $c)))."\n\n";
echo "str_replace ".($r = str_replace("$c", '', $a))."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $r)))."\n\n";
echo "preg_replace ".($r = preg_replace("/".preg_quote($c)."/", '', $a))."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $r)))."\n\n";
echo "preg_replace with unicode ".($r = preg_replace("/".preg_quote($c)."/u", '', $a))."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $r)))."\n\n";
$u = utf8_encode($c);
echo "replace char unicode ".$u."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $u)))."\n\n";
echo "preg_replace with unicode ".($r = preg_replace("/".preg_quote($u)."/u", '', $a))."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $r)))."\n\n";
$a[0] = "\xe1";
$a[1] = "\x7f";
echo "string with wrong utf-8 ".$a."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $a)))."\n\n";
$u = "\x7f";
echo "replace char unicode ".$u."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $u)))."\n\n";
echo "preg_replace with unicode ".($r = preg_replace("/".preg_quote($u)."/u", '', $a))."\n";
echo "bytes ".implode(' ', array_map('dechex', unpack('C*', $r)))."\n\n";
/*
encoding utf-8
string ᄿtest
bytes e1 84 bf 74 65 73 74
replace char ?
bytes 84
str_replace ?test
bytes e1 bf 74 65 73 74
preg_replace ?test
bytes e1 bf 74 65 73 74
PHP Warning: preg_replace(): Compilation failed: invalid UTF-8 string at offset 0 in /Users/hightemp/Projects/_PHP/str_replace/index.php on line 21
Warning: preg_replace(): Compilation failed: invalid UTF-8 string at offset 0 in /Users/hightemp/Projects/_PHP/str_replace/index.php on line 21
preg_replace with unicode
bytes
replace char unicode „
bytes c2 84
preg_replace with unicode ᄿtest
bytes e1 84 bf 74 65 73 74
replace char ??
bytes 84 bf
str_replace ?test
bytes e1 74 65 73 74
preg_replace ?test
bytes e1 74 65 73 74
PHP Warning: preg_replace(): Compilation failed: invalid UTF-8 string at offset 0 in /Users/hightemp/Projects/_PHP/str_replace/index.php on line 41
Warning: preg_replace(): Compilation failed: invalid UTF-8 string at offset 0 in /Users/hightemp/Projects/_PHP/str_replace/index.php on line 41
preg_replace with unicode
bytes
replace char unicode „¿
bytes c2 84 c2 bf
preg_replace with unicode ᄿtest
bytes e1 84 bf 74 65 73 74
string with wrong utf-8 ??test
bytes e1 7f bf 74 65 73 74
replace char unicode
bytes 7f
preg_replace with unicode
bytes
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment