Re: [PHP] modify an element of a HTML tag within a string

2002-07-24 Thread Analysis Solutions

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




[PHP] modify an element of a HTML tag within a string

2002-07-18 Thread Justin French

Hi all,

I've asked simular questions before, but I think I'm finally asking the
*right* question to get the right answer.

I'm look for some suggestions on the best method of parsing a HTML document
(or part thereof), with the view of CAPTURING and MODIFYING a specific
element of a specific tag.

something like:

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


I really don't know where to start.  I REALLY don't expect someone to write
this for me, just some guidance would be great -- or maybe some inspiration
:)


I guess what I'd like to be able to do, in pseudo code is:


while (getTag('DIV',$myHTML))
{
while(getAttribute('ID'))
{
// manipulate
if($attribute == foo)
{
$attribute == foo2;
}
}
}


while (getTag('DIV',$myHTML))
{
while(getAttribute('ID'))
{
deleteAttribute();
}
}


The solution might be a helluva lot more complex, or may be OOP based.


Any inspiration/links/words of wisdom?


Justin French


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