php - Replace letters on the whole page, but not within the tags -


i've been searching days.. no result.

i want replace latin letters cyrillic ones using php. want exclude words , letters within specific tag <notranslate>

so if have:

<p><b>ovo je neki tekst</b> ovo sigurno <notranslate>nece preci u cirilicu</translate>, hvala !</p> 

i want become:

<p><b>Ово је неки текст</b> и ово сигурно <notranslate>nece preci u cirilicu</translate>, хвала !</p> 

how this, using regex ?

there 2 problems in question:

1) how transliterate latin characters cyrillic character?

2) how preserve parts enclosed between "notranslate" tags?

for first question, haven't found way iconv. so, solution use strtr associative array latin characters associated cyrillic characters. note i'm not expert in cyrillic, feel free edit array fit needs.

the second problem easy solve using xpath query selects text nodes haven't tag "notranslate" ancestor.

$trans = [ 'a'=>'а', 'b'=>'б', 'v'=>'в', 'g'=>'г', 'd'=>'д', 'e'=>'е', 'e'=>'ё', 'zh'=>'ж', 'z'=>'з', 'i'=>'и', 'y'=>'й', 'k'=>'к', 'l'=>'л', 'm'=>'м', 'n'=>'н', 'o'=>'о', 'p'=>'п', 'r'=>'р', 's'=>'с', 't'=>'т', 'u'=>'у', 'f'=>'ф', 'h'=>'х', 'ts'=>'ц', 'ch'=>'ч', 'sh'=>'ш', 'sht'=>'щ', 'i'=>'ъ', 'y'=>'ы', 'y'=>'ь', 'e'=>'э', 'yu'=>'ю', 'ya'=>'я', 'a'=>'А', 'b'=>'Б', 'v'=>'В', 'g'=>'Г', 'd'=>'Д', 'e'=>'Е', 'e'=>'Ё', 'zh'=>'Ж', 'z'=>'З', 'i'=>'И', 'y'=>'Й', 'k'=>'К', 'l'=>'Л', 'm'=>'М', 'n'=>'Н', 'o'=>'О', 'p'=>'П', 'r'=>'Р', 's'=>'С', 't'=>'Т', 'u'=>'У', 'f'=>'Ф', 'h'=>'Х', 'ts'=>'Ц', 'ch'=>'Ч', 'sh'=>'Ш', 'sht'=>'Щ', 'i'=>'Ъ', 'y'=>'Ы', 'y'=>'Ь', 'e'=>'Э', 'yu'=>'Ю', 'ya'=>'Я' ];  $html = '<p><b>ovo je neki tekst</b> ovo sigurno <notranslate>nece <p>preci</p> u cirilicu</notranslate>, hvala !</p>';  $dom = new domdocument; @$dom->loadhtml('<div>' . $html . '</div>');  $xp = new domxpath($dom); $textnodes = $xp->query('//text()[not(ancestor::notranslate)]');  foreach ($textnodes $textnode) {    $textnode->nodevalue = strtr($textnode->nodevalue, $trans); }  $result ='';  foreach ($dom->getelementsbytagname('div')->item(0)->childnodes $childnode) {     $result .= $dom->savexml($childnode); }  echo $result; 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -