简体   繁体   English

PHP Regex在BBCode标签之间获取文本

[英]PHP Regex Get Text Between BBCode Tags

I need help with the PHP code for the following: 我需要以下PHP代码的帮助:

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

Long story short, I can't use CSS or DOM to do this, I need to do this on the server. 长话短说,我不能使用CSS或DOM来做到这一点,我需要在服务器上做到这一点。

#[code](.*?)[/code]# seems to only work if there are no breaks (or newlines) between the starting and ending tags.... :( #[code](。*?)[/ code]#似乎仅在开始和结束标记之间没有中断(或换行符)的情况下才有效。...:(

I think you're searching for something like this 我想您正在寻找这样的东西

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

hover, I'd suggest you to use BBcode parsers instead 悬停,建议您改用BBcode解析器


To replace all spaces with &nbsp; &nbsp;替换所有空格 , simply use preg_replace_callback ,只需使用preg_replace_callback

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM