Hi Justin:

On Fri, Jul 19, 2002 at 01:50:08PM +1000, Justin French wrote:
> 
> 1. look for a given tag eg DIV
> 2. capture the tag (everything from '<DIV' up to the '>')
> 3. look for a given attribute (eg ID="foo", ID=foo, ID='foo' -- all valid
> ways)
> 4. capture it
> 5. be given the opportunity to manipulate the attribute's value, delete it,
> etc
> 6. place captured tag (complete with modifed elements) back into the string
> in it's original position
> 7. return to step 1, looking for the next occurence of a DIV tag

This can be done with preg_replace() and a function.  Here are some 
variations on such an expression / function combo.

<?php

$Val = 'begin <div> middle <div id="idattr"> end';

function alter($var) {
   return "_$var" . '_';
}


# To grab all attributes...
$Out = preg_replace('/((<div)([^>]*))/i', '\\2' . alter("\\3"), $Val);
# \\1 contains whole match
# \\2 contains tag type
# \\3 contains attributes

echo htmlspecialchars($Out);

echo '<hr />';

# To grab just the id attribute...
$Out = preg_replace('/((<div) ?(id=(?:"|\')([^\'"]*)(?:"|\'))*)/i', '\\2' 
. alter("\\4"), $Val);
# \\2 contains tag type
# \\3 contains whole id attribute statement
# \\4 contains the attribute value

echo htmlspecialchars($Out);

?>

Of course, be a good hacker and modify it to suit your actual needs. :)

Enjoy,

--Dan

-- 
               PHP classes that make web design easier
        SQL Solution  |   Layout Solution   |  Form Solution
    sqlsolution.info  | layoutsolution.info |  formsolution.info
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7 Av #4AJ, Brooklyn NY     v: 718-854-0335     f: 718-854-0409

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to