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条)

报告相同问题?

悬赏问题

  • ¥30 数字信号处理实验报告
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程