I’m having a tough time learning regex and preg_split.
I’m trying to apply what i’ve learned and can’t seem to get a simple search going..
I’ve tried many variations, but can’t separate between the bold tags, and only bold tags
<?php
$string = "<b>this is</b> <i>not</b> <b>bold</b>";
$find = '/<b>/'; // works as expected, separating at <b>
$find = '/<b>|<\/b>/'; // works as expected, separating at either <b> or </b>
$find = '/<b>*<\/b>/'; // why doesn't this work?
$find = '/^<b>*<\/b>/'; // why doesn't this work?
$find = '/<b>.<\/b>/'; // why doesn't this work
$result = preg_split($find, $string);
print_r($result);
?>
As you can see, i’m trying to incorporate the . + or start ^/ finish $ characters.
What am I doing so very wrong where it isn’t working the way I expected?
Thanks for all your help!
p.s. found this which is very helpful too
Matches
"<b", zero or more">", followed by"</b>".Perhaps you meant this:
That would match
"<b>", followed by a string of unknown length, ending at the first occurrence of"</b>". I’m not sure why you would split on that though; applied on the above you would get an array of three elements:To match everything inside
"<b>"and"</b>"you needpreg_match_all():Do note that nested tags are not a great fit for regular expressions and you’d be wanting to use
DOMDocument.Matches
"<b"at the start of the string, zero or more">", followed by"</b>".Matches
"<b>", followed by any character, followed by"</b>".