Re: Regex to catch <p>s

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ryan S wrote:
 Hey all!

To say I suck at regex is an understatement so really need any help I can get on this, I have a page of text with different html tags in them, but each "block" of text has a <p> or a < class="something"> tag... anybody have any regex that will catch each of these paragraphs and put then into an array
example:
array[0]="<p> first block </p>";
array[1]="<p class="blah">  block X</p>";

Thanks!
R
Hi,

Maybe the example is overkill, but I give you a quick setup that can save you some time finding HTML tags with a certain attribute.

<?php

$html = <<<END_OF_HTML

<b>hello</b>
<b class="blah">hello</b>
<p>hello</p>
<p class="blah">hello</p>
<a>hello</a>
<a href="url">hello</a>
END_OF_HTML;

$tags = array();
$tags[] = 'p';
$tags[] = 'a';

$tags = implode('|', $tags);

$pattern = '/<('.$tags.')[^>]*>/i';

echo $pattern."\n";

preg_match_all($pattern, $html, $matches);

var_dump($matches);

?>

I'm not an expression guru either, but I think it works OK. I had to find 'link', 'img', 'a' and other tags in HTML and used a more complex expression for it which worked like a charm.

It's just an example. For you, you have to leave away the 'a' tag in the $tags array, to get what you want.

Hope it helps!
--

Aschwin Wesselius

/'What you would like to be done to you, do that to the other....'/

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux