preg_match and line breaks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

preg_match and line breaks

Post by AVATAr »

Im not very familiar with regular expressions (i have never work with them).. so here we go.

I wanna match words between tags. like this

Code: Select all

<tag1> hello world </tag1>
so I have tried this, but it dont match when line breaks appears within the text:

Code: Select all

<?php
$string = <<<END
<tag1>without linebrakes works ok!!!</tag1>
<tag2>After this a linebreak.
linebreak
linebreak
linebreak
didnt work!!!</tag2>
END;

preg_match('/<(tag1)>(.+?)<\/\1>/', $string, $match ); 
preg_match('/<(tag2)>(.+?)<\/\1>/', $string, $match2 ); 

echo '<pre>';
print_r($match);
print_r($match2); //no match!!!
echo '</pre>';
?>
can you help me with this. How can i match the text between tags that have line breaks? (i want to fetch ALL the text between them)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use the s modifer.
Post Reply