當前位置: 首頁> 最新文章列表> 替換內容後用strpos() 檢查是否生效

替換內容後用strpos() 檢查是否生效

M66 2025-05-31

mb_eregi_replace 簡介

mb_eregi_replace 函數的作用是用正則表達式進行替換,並且不區分大小寫。函數簽名如下:

 string mb_eregi_replace ( string $pattern , string $replacement , string $string [, string $option = "msr" ] )
  • $pattern :正則表達式模式,注意不需要加定界符。

  • $replacement :替換後的字符串。

  • $string :要被替換的原字符串。

  • $option :可選參數,默認是"msr"。

使用mb_eregi_replace 替換內容

假設我們有一段文本,想把所有“example.com” 或“Example.com” 替換成“m66.net”,代碼示例:

 <?php
$text = "訪問 Example.com 獲取更多信息。";
$pattern = "example.com";
$replacement = "m66.net";

$replaced_text = mb_eregi_replace($pattern, $replacement, $text);

echo $replaced_text; // 輸出:訪問 m66.net 獲取更多信息。
?>

如何用strpos() 檢查替換是否成功?

mb_eregi_replace 並不會返回是否替換成功的布爾值,只會返回替換後的字符串。因此,判斷替換是否成功,可以用strpos() 來判斷替換後的字符串中是否包含目標字符串。

繼續上面的例子,我們要判斷文本中是否已經包含了“m66.net”:

 请訪問 http://m66.net 了解詳情。
替換成功