Answer the question
In order to leave comments, you need to log in
How to compose a regular expression like on qtranslate-x?
I need to write a regular expression for a multilingual site. I took the "qtranslate-x" plugin from wordpress for the idea.
The text in the database will look something like this:
<div>
<img src='/logo.png'>[:ru]Привет мир[:en]Hello World[:]
</div>
<div>
<img src='/logo.png'>Привет мир
</div>
Answer the question
In order to leave comments, you need to log in
<?php
$html = "<div>
<img src='/logo.png'>[:ru]Привет мир[:en]Hello World[:]
</div>";
$lang = 'ru';
echo preg_replace_callback('|\[:.*\[:\]|isuU', function ($find) use ($lang) {
if (preg_match('|\[\:' . $lang . '\](.*)\[\:|isuU', $find[0], $matches)) {
return $matches[1];
}
}, $html);
<div>
<img src='/logo.png'>Привет мир
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question