Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I match a Russian word using preg_replace in PHP?
    primarykey
    data
    text
    <p>How do I go about matching a Russian word in a string (also in Russian) in PHP?</p> <p>So for example something like this:</p> <pre><code>$pattern = '/слово/'; preg_replace($pattern, $replacement, $string_in_russian) </code></pre> <p>I tried <code>utf8_encode</code> and <code>htmlentities</code> with UTF-8 flag for $pattern, but it didn't work. Should I also encode $string_in_russian?</p> <p><b>Update:</b> Suggestion for /u flag didn't work so I'm putting the actual code I need this for. It is from a glossary plugin for Wordpress (my site is properly setup to use Russian language, and it does work, but not in this instance). So here's the code</p> <pre><code>$glossary_title = $glossary_item-&gt;post_title; $glossary_search = '/\b'.$glossary_title.'s*?\b(?=([^"]\*"[^"]\*")\*[^"]*$)/iu'; $glossary_replace = '&amp;lt;a'.$timestamp.'&amp;gt;$0&amp;lt;/a'.$timestamp.'&amp;gt;'; $content_temp = preg_replace($glossary_search, $glossary_replace, $content, 1); </code></pre> <p>When I do a quick echo into HTML comment this is the kind of string I get for the pattern<br> <code>/\bсловоs*?\b(?=([^"]*"[^"]<em>")</em>[^"]*$)/iu</code></p> <p>And well, that still doesn't seem to work. I thought maybe it was the "s" that was screwing me over (this level of regex is a bit beyond me but I assume it's there for possible plurals), but removing it didn't help.</p> <p><b>Update #2:</b> Okay so I decided to do a complete "blank slate" test - plain PHP file with some $content strings in English and Russian and target words to replace. Here is the code</p> <pre><code>$content_en = 'Nulla volutpat pretium nunc, ac feugiat neque lobortis vitae. In eu sapien sit amet eros tincidunt viverra. &lt;b style="color:purple"&gt;Proin&lt;/b&gt; congue hendrerit felis, et consequat neque ultrices lobortis. &lt;b style="color:purple"&gt;Proin&lt;/b&gt; luctus bibendum libero et molestie. Sed tristique lacus a urna semper eget feugiat lacus varius. Donec vel sodales diam. &lt;b style="color:purple"&gt;Proin&lt;/b&gt; fringilla laoreet purus, a facilisis nisi porttitor vel. Nullam ac justo ac elit laoreet ullamcorper vel a magna. Suspendisse in arcu sapien.'; $find_en = 'proin'; $replace_with_en = '&lt;em style="color:red"&gt;REPLACEMENT&lt;/em&gt;'; $glossary_search = '/\b'.$find_en.'s*?\b(?=([^"]*"[^"]*")*[^"]*$)/iu'; $content_en_replaced = preg_replace($glossary_search, $replace_with_en, $content_en); $content_ru = 'Lorem Ipsum используют потому, что тот обеспечивает более или менее стандартное заполнение шаблона, а также реальное распределение букв и пробелов в абзацах, которое не получается при простой дубликации "Здесь &lt;b style="color:purple"&gt;ваш&lt;/b&gt; текст.. Здесь &lt;b style="color:purple"&gt;ваш&lt;/b&gt; текст.. Здесь &lt;b style="color:purple"&gt;ваш&lt;/b&gt; текст.." Многие программы электронной вёрстки и редакторы HTML используют Lorem Ipsum в качестве текста по умолчанию.'; $find_ru = 'ваш'; $replace_with_ru = '&lt;em style="color:red"&gt;Многие&lt;/em&gt;'; $glossary_search = '/\b'.$find_ru.'s*?\b(?=([^"]*"[^"]*")*[^"]*$)/iu'; $content_ru_replaced = preg_replace($glossary_search, $replace_with_ru, $content_ru); </code></pre> <p>And here is a screenshot of the output <a href="http://www.flickr.com/photos/iliadraznin/5372578707/" rel="nofollow">http://www.flickr.com/photos/iliadraznin/5372578707/</a></p> <p>As you can see the English text had the target word replaced, while the Russian hasn't and the code is identical and I'm using the /u flag. The file is also UTF-8 encoded. Any suggestions? (and again, I tried removing the "s", still nothing)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload