douzhang7603 2015-06-25 06:41
浏览 15
已采纳

php:preg_match和preg_replace

I'm unsure of how to do the following...

I need to search through a string and match all instances of a forward slash and certain letters. This is for word modifications that users have the ability to input and I want them to be able to modify individual words.

Here is an example string

Hello, isn't the weather just absolutely beautiful today!?

What I'd like the user to be able to do is something like this

Hello, isn't the /bo weather just /it beautiful today!?

take note of the /bo and /it

what I'd like to do is have a preg_match and or preg_replace statement that finds and replaces the instances of /bo and /it and converts them instead into html tags such as bolded html tag and italics html tag (i cant type them here or they get converted into actual html. but wrap themselves around the word immediately following the /bo so in this example it would wind up being

Hello, isn't the <b>weather</b> just <i>beautiful</i> today!?

Any ideas how I could do this with a regex?

Once the conversions are done i'll do the standard sanitizing before inserting the data into the database along with prepared statements.

  • 写回答

3条回答 默认 最新

  • duangu8264 2015-06-25 06:54
    关注
    $string = "Hello, isn't the /bo weather just /it beautiful /bo today!?";
    
    var_dump(preg_replace (array('/\/bo\s(\w+)/', '/\/it\s(\w+)/'), array('<b>$1</b>', '<i>$1</i>'), $string));
    

    "Hello, isn't the weather just beautiful today!?"

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

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!