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

  • 关于我们
  • 免责声明