简体   繁体   English

PHP正则表达式特殊字符

[英]php regex special character

I have: 我有:

$regExge = "/(?!((<.*?)))\b(Öffnung)\b(?!(([^<>]*?)>))/s";
$replacege = "<a href=''>Öffnung</a>";

And I used preg_replace to replace Öffnung string to html <a href=''>Öffnung</a> 然后我使用preg_replace将Öffnung字符串替换为html <a href=''>Öffnung</a>

$content = preg_replace($regExge, $replacege, $content);

But it not working, only working with normal string. 但它不起作用,仅适用于普通字符串。

There any way? 有办法吗 Thank you. 谢谢。

You need to indicate that your pattern should cover an encoding which includes the special characters. 您需要指出,您的模式应覆盖包含特殊字符的编码。 UTF-8 is one option here, and can be indicated by using /u at the end of the pattern: UTF-8是这里的一个选项,可以通过在模式末尾使用/u来表示:

$regExge = "/(?!((<.*?)))\b(Öffnung)\b(?!(([^<>]*?)>))/su";
$replacege = "<a href=''>Öffnung</a>";            //  ^^^
$content = preg_replace($regExge, "stuff", $replacege);
print $content;

<a href=''>stuff</a>

Demo 演示版

You can try this modified version. 您可以尝试此修改版本。

$content = "Offnung  Öffnung s;ldjfjkasÖffnung";
$regExge = "/Öffnung/";
$replacege = "<a href=''>Öffnung</a>";
$content = preg_replace($regExge, $replacege, $content);

echo $content;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM