Re: Regex to catch <p>s
| From: | Aschwin Wesselius | Date: | Tue, 06 May 2008 08:30:42 +0000 |
| Subject: | Re: Regex to catch <p>s | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-273935@lists.php.net to get a copy of this message | ||
Aschwin Wesselius wrote:
Aschwin Wesselius wrote:It is obvious I haven't had my caffeine yet. This is my last try to get the pattern straight: <?php $html = <<<END_OF_HTML <b>hello</b> <b class="blah">hello</b> <p>those</p> <p class="blah">hello</p> <a>hello</a> <a href="url">this</a> <a>rose</a> <a href="regex yo">hello</a> <a>nose</a> <a id="2" href="regex yo">hello</a> <p>that</p> <p class="blah" title="whatever">hello</p> END_OF_HTML; $tags = array(); $tags[] = 'p'; $tags[] = 'a'; $attr = array(); $attr[] = 'class'; $attr[] = 'href'; $vals = array(); $vals[] = 'blah'; $vals[] = 'url'; $vals[] = 'yo'; $text = array(); $text[] = 'hello'; $text[] = 'this'; $text[] = 'that'; $tags = implode('|', $tags); $attr = implode('|', $attr); $vals = implode('|', $vals); $text = implode('|', $text); $pattern = '/<('.$tags.')[^>]*('.$attr.')?[^>]*('.$vals.')?[^>]*>('.$text.')[^<\/]*<\/\1>/i'; echo $pattern."\n"; echo "--------------------\n"; preg_match_all($pattern, $html, $matches); var_dump($matches); ?> -- Aschwin Wesselius /'What you would like to be done to you, do that to the other....'/Ryan S wrote:Hi, I'm sorry. I didn't read your request properly. Below you'll have a correct solution: Hi,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! RHi, 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.