V
V
Vyacheslav Belyaev2016-01-23 08:35:41
PHP
Vyacheslav Belyaev, 2016-01-23 08:35:41

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>

You need to get this view when choosing the Russian language:
<div>
<img src='/logo.png'>Привет мир
</div>

Unfortunately, I couldn't find it in the source code of the plugin. I always had a problem with regular expressions, so don't send me to manuals. I hope there is a knowledgeable and understanding person who can help me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2016-01-23
@TroL929

<?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);

result
<div>
<img src='/logo.png'>Привет мир
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question