tags:

views:

35

answers:

1

Hello,

Consider the code below, why it is not working?

<?php

$str = "
<h4>
   title
</h4>
";

$result = preg_match_all ('/<h4>([\d\D])<\/h4>/mi', $str, $matches);
var_dump($matches);
+2  A: 
MvanGeest
+1 for s switch
Mark Baker
Maybe using .* instead of .+ in case of a empty node. Shouldn't be happening but depending on how HTML source is generated... mieux vaut prévenir que guérir
fabjoa
I removed the greediness, because that might match right up to the end tag of the very last h4 in the document! I hope to receive some feedback from Howard.
MvanGeest
Will it "probably include the newlines in your match"; I had no idea the matching was a matter of probablies.
salathe
No, it isn't. I meant "My personal opinion is that it will include the newlines in your match, but I have too little experience with PHP's PCRE (and I didn't test it), so I'm not sure if this will happen!" You're invited (by me, but more importantly, by SO) to edit my answer and confirm or deny the statement, or comment on it explaining if this will happen.
MvanGeest