change tag

9 views
Skip to first unread message

Luciano

unread,
Feb 23, 2010, 6:59:34 AM2/23/10
to phpQuery
hi,

I am trying to get a tag and replace it by another.

<temp01 attr01="0123" attr02="0123" attr03="0123">content</temp01>

exchange for:

<script attr01="0123" attr02="0123" attr03="0123">content</script>

detail all content and all attributes must remain the same.

Luciano

unread,
Feb 24, 2010, 1:33:29 PM2/24/10
to phpQuery
I managed to find a solution. I don't know if this is really
the right one. I noticed that there is problems recursion.

$doc["prog"]->attr('new_tag_name','script');
$doc["prog"]->map('change_tag_name');

print $doc;


function change_tag_name($item)
{
$new_tab_name = trim(pq($item)->attr('new_tag_name'));
if (! empty($new_tab_name)){
$attrs = pq($item)->attr('*');
$content = pq($item)->html();
pq($item)->after("<$new_tab_name>$content</$new_tab_name>");//-
>attr($attrs);
unset ($attrs['new_tag_name']);
if ( is_array($attrs))
foreach ($attrs as $key => $value )
pq($item)->next()->attr($key,$value);
pq($item)->remove();
}
}


If anyone has any suggestions?

Max Kenten

unread,
Feb 24, 2010, 3:29:57 PM2/24/10
to phpQuery
It is probably somewhat fast to use regex here.
Replace the complete tag with the result of the following code:

$newTag = preg_replace('/<([a-z0-9]+)([^>]*)>([^<]*)</\1>/', '<newtag
$2>$3</newtag>', $oldTag);

Reply all
Reply to author
Forward
0 new messages