简体   繁体   English

PHP Preg_Replace REGEX BB 代码

[英]PHP Preg_Replace REGEX BB-Code

So I have created this function in PHP to output text in the required form.所以我以所需的形式在 PHP 到 output 文本中创建了这个 function。 It is a simple BB-Code system.这是一个简单的 BB 代码系统。 I have cut out the other BB-Codes from it to keep it shorter (Around 15 cut out)我已经从中删除了其他 BB 代码以使其更短(大约删除了 15 个)

My issue is the final one [title=blue]Test[/title] (Test data) does not work.我的问题是最后一个[title=blue]Test[/title](测试数据)不起作用。 It outputs exactly the same.它输出完全相同。 I have tried 4-5 different versions of the REGEX code and nothing has changed it.我已经尝试了 4-5 个不同版本的 REGEX 代码,但没有任何改变。

Does anyone know where I am going wrong or how to fix it?有谁知道我哪里出错或如何解决?

function bbcode_format($str){
$str = htmlentities($str);
$format_search =  array(
'#\[b\](.*?)\[/b\]#is',
'#\[title=(.*?)\](.*?)\[/title\]#i'
);
$format_replace = array(
'<strong>$1</strong>',
'<div class="box_header" id="$1"><center>$2</center></div>'
);
$str = preg_replace($format_search, $format_replace, $str);
$str = nl2br($str);
return $str;
}

Change the delimiter # to / .将分隔符#更改为/ And change " /[/b\] " to " \[\/b\] ".并将“ /[/b\] ”更改为“ \[\/b\] ”。 You need to escape the "/" since you need it as literal character.您需要转义“/”,因为您需要它作为文字字符。

Maybe the " array() " should use brackets: " array[] ".也许“ array() ”应该使用方括号:“ array[] ”。

Note: I borrowed the answer from here: Convert BBcode to HTML using JavaScript/jQuery注意:我从这里借用了答案: Convert BBcode to HTML using JavaScript/jQuery

Edit: I forgot that "/" isn't a metacharacter so I edited the answer accordingly.编辑:我忘记了“/”不是元字符,所以我相应地编辑了答案。

Update: I wasn't able to make it work with function, but this one works.更新:我无法让它与 function 一起使用,但是这个可以。 See the comments.查看评论。 (I used the fiddle on the accepted answer for testing from the question I linked above. You may do so also.) Please note that this is JavaScript. You had PHP code in your question. (我在接受的答案上使用了小提琴来测试我上面链接的问题。你也可以这样做。)请注意这是 JavaScript。你的问题中有 PHP 代码。 (I can't help you with PHP code at least for awhile.) (至少在一段时间内,我无法帮助您使用 PHP 代码。)

$str = 'this is a [b]bolded[/b], [title=xyz xyz]Title of something[/title]';

//doesn't work (PHP function)
//$str = htmlentities($str);

//notes: lose the single quotes
//lose the text "array" and use brackets
//don't know what "ig" means but doesn't work without them
$format_search =  [
/\[b\](.*?)\[\/b\]/ig,
/\[title=(.*?)\](.*?)\[\/title\]/ig
];

$format_replace = [
  '<strong>$1</strong>',
  '<div class="box_header" id="$1"><center>$2</center></div>'
];

// Perform the actual conversion
for (var i =0;i<$format_search.length;i++) {
  $str = $str.replace($format_search[i], $format_replace[i]);
}

//place the formatted string somewhere
document.getElementById('output_area').innerHTML=$str;

Update2: Now with PHP... (Sorry, you have to format the $replacements to your liking. I just added some tags and text to demostrate the changes.) If there's still trouble with the "title", see what kind of text you are trying to format.更新 2:现在使用 PHP...(抱歉,您必须根据自己的喜好格式化$replacements 。我只是添加了一些标签和文本来演示更改。)如果“标题”仍然存在问题,请查看什么样的文本您正在尝试格式化。 I made the title "=" optional with ?我将标题“=”设为可选的? so it should work properly work texts like: "[title=id with one or more words]Title with id[/title]" and "[title]Title without id[/title]. Not sure thought if the id attribute is allowed to have spaces, I guess not: http://reference.sitepoint.com/html/core-attributes/id .因此它应该可以正常工作文本,例如:“[title=id with one or more words]Title with id[/title]”和“[title]Title without id[/title]”。不确定是否允许使用id属性有空格,我猜不是: http://reference.sitepoint.com/html/core-attributes/id

$str = '[title=title id]Title text[/title] No style, [b]Bold[/b], [i]emphasis[/i], no style.';

//try without this if there's trouble
$str = htmlentities($str);

//"#" works as delimiter in PHP (not sure abut JS) so no need to escape the "/" with a "\"
$patterns = array();
$patterns = array(
  '#\[b\](.*?)\[/b\]#',
  '#\[i\](.*?)\[/i\]#', //delete this row if you don't neet emphasis style
  '#\[title=?(.*?)\](.*?)\[/title\]#'
);

$replacements = array();
$replacements = array(
  '<strong>$1</strong>',
  '<em>$1</em>', // delete this row if you don't need emphasis style
  '<h1 id="$1">$2</h1>'
);

//perform the conversion
$str = preg_replace($patterns, $replacements, $str);
echo $str;

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

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