duanshan3331 2011-05-16 23:48
浏览 24
已采纳

带替换功能的preg_replace

How do you use preg_replace with a function as the replacement parameter? I'm getting an error with this code.

function getInfo($id,$slot){
  if(!$id){ return '<b>Error</b> Id Not Returned. Please contact support@site.com for more information.'; }
  $mm = mysql_query("SELECT * FROM `users` WHERE `id`='".$id."'");
  $mma = mysql_fetch_assoc($mm);
  $p = $mma[$slot];
  return $p;
  //return $id; <- Debug (Returns ID given)
}
$post = preg_replace(
  "/\[CallName]([^]]+)\[\/CallName\]/", 
  getInfo('\\1',"fullname"), 
  $post
);
  • 写回答

2条回答 默认 最新

  • douguai7291 2014-01-21 21:44
    关注

    The e modifer is now deprecated in favor of preg_replace_callback.

    Sample usage:

    $x = 'abcd-efg-hijk-lmnop';
    
    $x = preg_replace_callback(
      '/-(.)/', //pattern
      function($matches) { //callback
        return strtoupper($matches[1]);
      }, 
      $x //subject
    );
    
    echo $x; //abcdEfgHijkLmnop
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。