PHP Regex在BBCode标签之间获取文本 [英] PHP Regex Get Text Between BBCode Tags

查看:68
本文介绍了PHP Regex在BBCode标签之间获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以下PHP代码方面的帮助:

I need help with the PHP code for the following:

获取给定字符串中每次出现的BBCode标签[code]和[/code]之间的文本,这样我就可以用nbsp字符替换空格''.

Get the text between each occurrence the BBCode tags [code] and [/code] in a given string so I can then replace the spaces ' ' with the nbsp character.

长话短说,我不能使用CSS或DOM来做到这一点,我需要在服务器上做到这一点.

Long story short, I can't use CSS or DOM to do this, I need to do this on the server.

#[code](.*?)[/code]#似乎仅在开始和结束标记之间没有中断(或换行符)的情况下才起作用....:(

#[code](.*?)[/code]# seems to only work if there are no breaks (or newlines) between the starting and ending tags.... :(

推荐答案

我认为您正在搜索类似内容

I think you're searching for something like this

<?php
preg_match_all("/\[code\](.*?)\[\/code\]/ism", $search, $match);

悬停,建议您改用BBcode解析器

hover, I'd suggest you to use BBcode parsers instead

要将所有空格替换为&nbsp;,只需使用preg_replace_callback

To replace all spaces with &nbsp;, simply use preg_replace_callback

<?php
$text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism", function($match) {
     return str_replace(" ", "&nbsp;", $match[1]);
}, $search);

这篇关于PHP Regex在BBCode标签之间获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆