Index > Test > Accessibility > Server > Php > PHP Replace

str_replace vs. preg_replace

Small, probably meaningless comparison between PHP[g] functions str_replace and preg_replace in processing of common string (un-recommended in the case of preg_replace: no regular expression search or replace: text only and result could be... well... not that one which you expected).

Enter the URL of web-page which is in Russian and choose correct encoding or use ready samples bellow; all page will be transliterated (cyrillic letters to latinic).

Charset:

Ready samples
  1. Skotskojj Bunt:
  2. Skotnyjj Dvor: Skazka [Str_Replace] [Preg_Replace] (file size: 0.15 KiB)
  3. Dni v Birme [Str_Replace] [Preg_Replace] (file size: 0.15 KiB)

Code

<!-- -------- Code start ---- -->
$cyrillic=array(cyrillic alphabet);
$tliter=array(transliterated);
function &cyr2latStr($str) {
      return str_replace($cyrillic,$tliter,$str);
// or return preg_replace($cyrillic,$tliter,$str);
}
$string=implode('',file($fileName));
print cyr2latStr($string);
<!-- -------- Code end ---- -->

References

Functions:
http://php.net/str_replace
http://php.net/preg_replace
Transliteration:
URL: Transliteration official rules (in Russian)

[orWell.ru Tests] [Accessibility] [Server] [PHP] ~ [CSS off]

[orwell.ru] [Home] [Biography] [Library] [A Life] [Info & (c)] [Links] [Site map] [Search] [Feedback]

© 1999-2004 O. Dag – ¡C. date: 2003-12-09 & L. mod.: 2019-12-29!