Jump to content

regular express for tag


tomindo

Recommended Posts

preg_match_all('~<p>.*?</p>~',$html,$ptags);
$ptags = $ptags[0];

 

example:

$html = "<p>test</p>
<p>test1</p>
<p>test2</p>";
preg_match_all('~<p>.*?</p>~',$html,$ptags);
$ptags = $ptags[0];
echo "<pre>";
print_r($ptags);

// output:
Array
(
    [0] => <p>test</p>
    [1] => <p>test1</p>
    [2] => <p>test2</p>
)

All php regex patters have to have a delimiter wrapped around the pattern.  The tilde is just being used as the pattern delimiter.  Other than that, it has no special significance.  You can use most any non-alphanumeric character as the pattern delimiter, as long as you make sure to escape instances of it within your pattern.  I use the tilde because it rarely ever comes up in stuff you are trying to match.  Especially when making a pattern that matches html code. Lots of people use /.../ well in html code there are a lot of forward slashes, so you have to escape it every time you use it in your pattern. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.