duanmao2650 2015-07-01 19:13
浏览 39
已采纳

子字符串拆分 - PHP

So, I guess that this is a pretty simple concept, but I am unsure as to how I would achieve my intended result. What I am wanting, is for words which start with the '@' symbol, to be outputted with a <span> encasing them.

Let's say that the following is the whole string:

Mark wants the new app to be released on Friday, but some assets need refining so that they fit the theme @design_team.

How would I capture the...

@design_team

...sub-string, bearing in mind that characters other than an underscore should not be accounted for in the sub-string, to help keep the format.

Please let me know if this is possible with PHP, and if so, how.

  • 写回答

4条回答 默认 最新

  • duanmengmiezen8855 2015-07-01 19:19
    关注

    Use preg_replace:

    $string = preg_replace('/@\w+/', '<span>$0</span>', $string);
    

    \w matches word characters (letters, numbers, underscore), + makes it match a sequence of them. And in the replacement string $0 gets the matching substring.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 docker环境配置
  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现