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>
)

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.