Search code examples
phpregexpreg-split

Having trouble with preg_split and using regular expressions


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


Solution

  • $find = '/<b>*<\/b>/'; // why doesn't this work?
    

    Matches "<b", zero or more ">", followed by "</b>".

    Perhaps you meant this:

    $find = '/<b>.*?<\/b>/';
    

    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:

    " "
    "<i>not</b> "
    ""
    

    To match everything inside "<b>" and "</b>" you need preg_match_all():

    preg_match_all('#<b>(.*?)</b>#i', $str, $matches);
    // $matches[1] will contain the patterns inside the bold tag, theoratically
    

    Do note that nested tags are not a great fit for regular expressions and you'd be wanting to use DOMDocument.


    $find = '/^<b>*<\/b>/'; // why doesn't this work?
    

    Matches "<b" at the start of the string, zero or more ">", followed by "</b>".

    $find = '/<b>.<\/b>/'; // why doesn't this work
    

    Matches "<b>", followed by any character, followed by "</b>".