doudou8783 2016-01-15 20:37
浏览 52
已采纳

如何匹配字符串中的字符,并使用正则表达式在每个字符旁边放置一个点

Please see my code below.

this returns John Doe L. R T

I want it to return John Doe L. R. T.

$string = "John Doe L R T";

$matches = null;
preg_match('/\b\s[a-zA-z]{1}\b/i', $string, $matches);

foreach ($matches as $match)
{
    $string = str_replace($match, $match.'.',$string);
}

echo $string;
  • 写回答

2条回答 默认 最新

  • duanji1899 2016-01-15 20:46
    关注

    You can use preg_replace for that

    preg_replace('/\b\s[a-zA-z]{1}\b/i', '$0.', $string);
    

    The result would be

    John Doe L. R. T.

    And you'll probably want to replace that for uppercase-letters only (since they are likely to be names), so you should not use the i flag.

    preg_replace('/\b\s[A-z]{1}\b/', '$0.', $string);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒