duangengruan2144 2013-01-22 15:38
浏览 68
已采纳

正则表达式删除空格并更改标点符号?

Alright, so this is linked to a previous question I had. I am very new to PHP in general, and simply only need it to fix something someone else did to work for a forum of mine.

This is what I currently have:

// Alter post output to create links to user profiles
$post->message = preg_replace(
   '/@ *('.preg_quote($speak['username'], '/').')[:,.]? */i',
'[url=\''.PAGE_URL.RELATIVE_WBB_DIR.'/index.php?'
. 'page=User&userID='.$speak['toID'].'\']@'.$ speak['username'].':[/url]',
                              $post->message);

This should change:

@JamesNorris, @JamesNorris: @JamesNorris

to user profile links, and it does. The only problem is that the other change it makes is this:

@JamesNorris:@JamesNorris:@JamesNorris:

That is the output from that line given before. Apparently it is removing the ',' and whitespace, and placing a ':'.

Does anyone have any ideas on how to fix this problem?

  • 写回答

1条回答 默认 最新

  • douzoudang1511 2013-01-22 15:43
    关注

    You can capture the ,:. as its own group and reference that in the replacement:

    ('preg_quote(...)')([:,. ])? */i',
       '[url...'.$speak['username'].'\2[/url]'
    

    The \2 is whatever was captured in the second group (colon, comma, period, space). It's \2 because you already have another capture group for the username (that you don't use).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊