Re: Regex to catch <p>s

From: Date: Tue, 06 May 2008 07:48:46 +0000
Subject: Re: Regex to catch <p>s
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-273932@lists.php.net to get a copy of this message
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....'/

« previous php.general (#273932) next »