简体   繁体   中英

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>

$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:

$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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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