PHP Regex Get Text Between Bbcode Tags

PHP Regex Get Text Between BBCode Tags

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


To replace all spaces with  , simply use preg_replace_callback

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

Regex to match content between bbcode-like tags

If I don't misunderstand your question, then you can do this way to capture the internal text content using this regex.

<?php

$re = '/(?<=\])(.*?)(?=\[\/)/m';
$str = '[test_tag with="attributes"]Content I [want] To capture[/test_tag]';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
echo $matches[0][0];
?>

WORKING DEMO: https://3v4l.org/qgcNg

Using a regular expression to extract text in between BBCode-style tags

\[SECTION\](.*?)\[/SECTION\]

I think this is what you want, getting the text for the contents of a single SECTION?

The ? makes the * lazy, so it will only match up to the first [/SECTION] from the current one.


Example:

$input = "[CONTENT][SECTION]This is the section C #1[/SECTION][SECTION]This is the section C #2[/SECTION][SECTION]This is the section E #3[/SECTION]";
var_dump(preg_match_all("(\[SECTION\](.*?)\[/SECTION\])",$input,$m),$m);

Result:

int(3)
array(2) {
[0]=>array(3) {
[0]=>string(43) "[SECTION]This is the section C #1[/SECTION]"
[1]=>string(43) "[SECTION]This is the section C #2[/SECTION]"
[2]=>string(43) "[SECTION]This is the section E #3[/SECTION]"
}
[1]=>array(3) {
[0]=> string(24) "This is the section C #1"
[1]=> string(24) "This is the section C #2"
[2]=> string(24) "This is the section E #3"
}
}

Regex excluding matches wrapped in specific bbcode tags

Because JS lacks backtracking verbs you will need to consume those bracketed chunks but later replace them as is. By obtaining the second side of the alternation from your own regex the final regex would be:

\[(quote|i?code)[^\]]*\][\s\S]*?\[\/\1\]|(?![^<]*>|[^\[]*\])"([^"]*)"

But the tricky part is using a callback function with replace() method:

str.replace(regex, function($0, $1, $2) {
return $1 ? $0 : '“' + $2 + '”';
})

Above ternary operator returns $0 (whole match) if first capturing group exists otherwise it encloses second capturing group value in curly quotes and returns it.

Note: this may fail in different cases.

See live demo here

How to get attribute from bbcode [URL] tag using regex

I think what you want is preg_match?

Actually, you can just use the pattern you already have. It will give you the whole match and each match within round parantheses through a parameter.

preg_match('/\[url=(.+?)\](.+?)\[\/url\]/i', $string, $matches);
echo $matches[1]; // http://www.google.com
echo $matches[2]; // Google
echo $matches[0]; // [URL="http://www.google.com"]Google[/URL]

The return value of preg_match is 1 (match found) or 0/false (no match/error).



Related Topics



Leave a reply



Submit