logo
  • 教程列表
  • SO官方文檔
  • 正規表示式(regexp PCRE)
    • 全域性 RegExp 匹配
    • 字串與正規表示式匹配
    • 通過正規表示式將字串拆分為陣列
    • 用正規表示式替換字串
    • 字串替換為回撥
  1. StackOverflow 文件
  2. PHP 教程
  3. 正規表示式(regexp PCRE)
  4. 用正規表示式替換字串

用正規表示式替換字串

Created: November-22, 2018

$string = "a;b;c\nd;e;f";
// $1, $2 and $3 represent the first, second and third capturing groups
echo preg_replace("(^([^;]+);([^;]+);([^;]+)$)m", "$3;$2;$1", $string);

輸出

c;b;a
f;e;d

搜尋分號之間的所有內容並反轉順序。

  • 字串替換為回撥
  • 通過正規表示式將字串拆分為陣列

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 關於本站
  • 免責聲明