Re: [PHP] Regex to catch <p>s

This is a discussion on Re: [PHP] Regex to catch <p>s within the PHP General forums, part of the PHP Programming Forums category; <clip> > To say I suck at regex is an understatement so really need any help I can ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-06-2008
Ryan S
 
Posts: n/a
Default Re: [PHP] Regex to catch <p>s



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



If you're using php5 you can use DOM's getElementsByTagName.

If you still think you need to do some sort of regex it is possible
but it will be buggy at best.


</clip>

Nope, need a regex... guess I have no choice, either chancy regex or nothing... I know for a fact that the first paragraph tag wont contain a class, and for the <p> tags that contain a class="blah" does it matter that i know exactly what the classname is?



__________________________________________________ __________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ
Reply With Quote
  #2 (permalink)  
Old 05-06-2008
Shawn McKenzie
 
Posts: n/a
Default Re: [PHP] Regex to catch <p>s

Ryan S wrote:
>
> <clip>
>> 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

>
>
> If you're using php5 you can use DOM's getElementsByTagName.
>
> If you still think you need to do some sort of regex it is possible
> but it will be buggy at best.
>
>
> </clip>
>
> Nope, need a regex... guess I have no choice, either chancy regex or nothing... I know for a fact that the first paragraph tag wont contain a class, and for the <p> tags that contain a class="blah" does it matter that i know exactly what the classname is?
>
>
>
> __________________________________________________ __________________________________
> Be a better friend, newshound, and
> know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ


preg_match_all('|<p[^>]*>(.*)</p>|Ui', $myText, $myArray);
Reply With Quote
  #3 (permalink)  
Old 05-06-2008
vester_s
 
Posts: n/a
Default Re: Re[PHP] gex to catch <p>s


$tag_regex=array(
'/\<p(\s*)\>(.*?)\<\/p\> /si' => "$1",
'/\<(\s*)(*.?)class\=(*.?)\>(.*?)\<\/(*.?)\>/si' => "$3"
);

$paragraphs=preg_replace(array_keys($tag_regex),ar ray_values($tag_regex),$page);

I am not sure what tag is that you mean on <class="something">, but in this
RE .. it should capture any <p> tags (the first element of the array) and
any tags (the second element of the array) that has attribute class on it.

You can find another example of this kind of HTML parsing in the PHP... try
googling it..:)

HTH


Ryan S-4 wrote:
>
>
>
> <clip>
>> 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

>
>
> If you're using php5 you can use DOM's getElementsByTagName.
>
> If you still think you need to do some sort of regex it is possible
> but it will be buggy at best.
>
>
> </clip>
>
> Nope, need a regex... guess I have no choice, either chancy regex or
> nothing... I know for a fact that the first paragraph tag wont contain a
> class, and for the <p> tags that contain a class="blah" does it matter
> that i know exactly what the classname is?
>
>
>
>
> __________________________________________________ __________________________________
> Be a better friend, newshound, and
> know-it-all with Yahoo! Mobile. Try it now.
> http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>


--
View this message in context: http://www.nabble.com/Re%3A-Regex-to...p17089906.html
Sent from the PHP - General mailing list archive at Nabble.com.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:36 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0