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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
<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 |
|
|||
|
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); |
|
|||
|
$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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|