PHP, preg_replace, replace tag with tag attr

后端 未结 2 1537
谎友^
谎友^ 2021-01-27 06:09

Can I have a detail explain about how can I replace with tag with current attr using php?

I read manual and some referencs

How to use php preg_replace to replac

2条回答
  •  -上瘾入骨i
    2021-01-27 06:34

    It is almost never a good idea to use regular expressions for HTML. Consider DOM, then something like:

    foreach($dom->getElementsByTagName('script') as $script) {
        $temp = $dom->createElement('temp');
        /*
         * create src attribute, append it to $temp and copy value from $script
         * I leave that up to you.
         */
        $script->parentNode->replaceChild($temp, $script);
    }
    

提交回复
热议问题