0

Sorry i'm new for regexp, so i need a regexp for this type of tag, exactly for this:

<p style="font-family: Georgia;">Text</p>

I have a regexp that suits for all p tags, regexp for all p tag is: <\s*p[^>]*>([^<]*)<\s*\/\s*p\s*>/ found it on this site. I tried to figure out by myself, i wrote this: <\s*p[\"\^font\-\family\:\()\Georgia\;\$\"\]*>([^<]*)<\s*\/\s*p\s*>/ but i know it's wrong can anyone help me :) I need regexp exactly for this piece: style="font-family: Georgia;" Thank you.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
AGO
  • 1
  • Why not use a HTML parser? Writing the regexps yourself may not be the best idea... – Vincent Savard Aug 13 '11 at 20:48
  • Check [this](http://stackoverflow.com/questions/6058986/match-all-text-between-two-html-tags-using-regex-php/6059099#6059099) please. – SIFE Aug 13 '11 at 20:49
  • *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Aug 13 '11 at 21:09

1 Answers1

1

If you just want to match style="font-family: Georgia;" use this: \w+=".+"

For matching the the entire thing \s*<p \w+=".+">([^<]*)</p>\s* can do the trick. This will capture all paragraphs with exactly one attribute with a quoted value.. Not really flexible. If you only want to capture on paragraphs with exactly the font-family georgia style use \s*<p style="font-family: Georgia;">([^<]*)</p>\s*

If you find yourself using a lot of regular expressions to "parse" html, a HTML parser might save yourself a lot of trouble. Php's DOM class can parse html.

kjetilh
  • 4,821
  • 2
  • 18
  • 24
  • It don't work for me, i use as you said: if(preg_match_all('\s*

    ([^<]*)

    \s*', $result, $ptag)) { foreach($ptag[0] as $p) { echo $p."
    "; } } It drops that -> Warning: preg_match_all() [function.preg-match-all]: Delimiter must not be alphanumeric or backslash
    – AGO Aug 13 '11 at 22:37
  • yes you need two include the start/end delimiters of the regex, as such: #\s*

    ([^<]*)

    \s*#
    – kjetilh Aug 14 '11 at 10:52