regex for html tag

this is pissing me off :(

Page 1 of 1

6 Replies - 28623 Views - Last Post: 23 February 2006 - 10:34 PM Rate Topic: -----

#1 theRemix   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 440
  • Joined: 19-October 05

regex for html tag

Posted 22 February 2006 - 10:28 PM

it's been really hard to find a good regex that matches html, and then i found one, but it doesn't work with php

can someone please help me with are regex that can match any simple html tag?

this is the one i found, it works fine with dreamweaver's find using regular expressions, it finds all my tags.

</?\w+((\s+\w+(\s*=\s*(?:\".*?\\\"|.*?|[^">\s]+))?)+\s*|\s*)/?>

or this
</?\w+\s+[^>]*>

thanks.

This post has been edited by theRemix: 22 February 2006 - 10:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: regex for html tag

#2 snoj   User is offline

  • Married Life
  • member icon

Reputation: 93
  • View blog
  • Posts: 3,583
  • Joined: 31-March 03

Re: regex for html tag

Posted 22 February 2006 - 11:07 PM

Why wouldn't a regexp work with PHP? I see no reason why it wouldn't. And what was that regexp?
Was This Post Helpful? 0
  • +
  • -

#3 theRemix   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 440
  • Joined: 19-October 05

Re: regex for html tag

Posted 22 February 2006 - 11:16 PM

heres my code using my first example:

$theString = '
      <p style="margin-left: 0; margin-right: 12" align="left"><b><font size="4" color="#0066CC" face="Arial">A</font></b></p>
      <p style="margin-left: 0; margin-right: 12; margin-bottom: 10" align="left"><font color="#0066CC" face="Arial" size="3">To
      see the letter &quot;A&quot; in your dream, represents superiority. You
      are moving on to something new and grand. Alternatively, it may indicate
      the name or initial of a person.</font></p>
      <p style="margin-left: 0; margin-right: 12; margin-bottom: 10" align="left"><b><font size="4" color="#0066CC" face="Arial">Abandonment</font></b></p>
      <p style="margin-left: 0; margin-right: 12; margin-bottom: 10" align="left"><font color="#0066CC" face="Arial" size="3">To
      dream that you are abandoned, suggests that it is time to leave behind
      past feelings and characteristics that are hindering your own growth. Let
      go of your old attitudes. A more direct and literal interpretation of this
      dream is that you have a fear of being deserted, abandoned, or even
      betrayed. Do you feel that people are neglecting your feelings?&nbsp;</font></p>';

$reg = '</?\w+((\s+\w+(\s*=\s*(?:\".*?\\\"|.*?|[^">\s]+))?)+\s*|\s*)/?>';
   $theArray = split($reg, $theString);
    foreach ($theArray as &$value) {
      echo "$value ######################### ";
  }



heres the error
Warning: split() [function.split]: REG_BADRPT in /home/www/my.php on line 440

Warning: Invalid argument supplied for foreach() in /home/www/my.php on line 443



oh yah and the server is running php5 if that matters.

This post has been edited by theRemix: 22 February 2006 - 11:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 gayatri   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

Re: regex for html tag

Posted 23 February 2006 - 12:19 AM

i think u need the html code within tag
n hence wanna match the tags
is that right??
i guess u can use
regex as

preg_match_all("/[\s]*[<html>][\s]*[\w]*[\s]*[<\\html>][\s]*/", $string, $matches);

try this out, i hope it works

This post has been edited by gayatri: 23 February 2006 - 12:44 AM

Was This Post Helpful? 0
  • +
  • -

#5 snoj   User is offline

  • Married Life
  • member icon

Reputation: 93
  • View blog
  • Posts: 3,583
  • Joined: 31-March 03

Re: regex for html tag

Posted 23 February 2006 - 12:44 AM

Have you tried preg_split instead? It works for me.

preg-ed expression (just added some \'s to escape /'s and the 's' at the end).
/<\/?\w+((\s+\w+(\s*=\s*(?:\".*?\\\"|.*?|[^">\s]+))?)+\s*|\s*)\/?>/s


Was This Post Helpful? 0
  • +
  • -

#6 theRemix   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 440
  • Joined: 19-October 05

Re: regex for html tag

Posted 23 February 2006 - 05:51 PM

worked perfect, thanks everyone.
Was This Post Helpful? 0
  • +
  • -

#7 gayatri   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

Re: regex for html tag

Posted 23 February 2006 - 10:34 PM

yup,
hotsnoj ur's was a better reply
i tried it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1