d-_-b
d-_-b

Reputation: 23171

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

Upvotes: 1

Views: 333

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173562

$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>".

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

The first two "why doesn't this work" are matching <b followed by zero or more > characters, followed by </b>. The last one matches <b> then any single character then </b>.

I'm not sure what you're trying to do exactly, but this would split on start and end bold tags: <\/?b> - it matches <, followed by an optional /, followed by b>.

Upvotes: 3

Related Questions