faqts : Computers : Programming : Languages : PHP : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 5 people (100%) answered Yes
Recently 5 of 5 people (100%) answered Yes

Entry

How do I find "tagname", "attributen", and "data" from "<tagname attributename=attributevalue>data</

Dec 1st, 2003 12:59
Jakub Vrana, Elie Medeiros,


If it is as simple as in the question, it's not too difficult. It 
became more difficult if inside <> can be more attributes, values can 
be quoted or unqoted, e.t.c.

<?php
ereg("<([^ ]*) ([^=]*)=([^>]*)>([^<]*)</", $string, $regs);
/*
for "<tag attribute=value>data</" will be in $regs:
$regs[1] = "tag";
$regs[2] = "attribute";
$regs[3] = "value";
$regs[4] = "data";
*/
?>

This will catch only first tag. If you want to catch all tags, take a 
look at preg_match_all().