簡體   English   中英

PHP正則表達式特殊字符

[英]php regex special character

我有:

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

然后我使用preg_replace將Öffnung字符串替換為html <a href=''>Öffnung</a>

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

但它不起作用,僅適用於普通字符串。

有辦法嗎 謝謝。

您需要指出,您的模式應覆蓋包含特殊字符的編碼。 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>

演示版

您可以嘗試此修改版本。

$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