简体   繁体   中英

How can I remove all content between <pre> tags in PHP?

I've read and tried to implement variations on about 10 different solutions for this so far from stack overflow, and none of them are working. All I want to do is replace the content between two pre tags (including the tags themselves). I don't care if it's regex or straight up php. Anyone have any suggestions?

An example is:

This is how to remove pre tags and their contents:<br/>

<pre>
<?php>
[code here]
<?php>

That's all there is to it.

becomes:

This is how to remove pre tags and their contents:</br>
That's all there is to it.

This needs to happen before the html is rendered to the page.

I'm not sure DOMDocument will work. The context for my code is that it is happening within a plugin for expression engine (a codeigniter / php based CMS). The plugin truncates the html to a set character length, and renders that back to the parent template to be rendered in the browser - so the domdocument can't render to the browser - it just needs to return the code to the parent template with the tags and content removed.

Use DOMDocument :

$html = '<div id="container">
    <div id="test"></div>
    <pre>
        content
    </pre>
</div>';

$dom = new DOMDocument;
$dom->loadXML($html);

$xpath = new DOMXPath($dom);
$query = '//div[@id="container"]/pre';
// $query = '//pre'; // for all <pre>
$entries = $xpath->query($query);

foreach($entries as $one){
    $newelement = $dom->createTextNode('Some new node!'); 
    $one->parentNode->replaceChild($newelement, $one);
}

echo $dom->saveHTML();

Codepad Example

Regex will work fine if you use assertions (ie lookahead/lookbehind). This should remove anything within pre tags:

$page_content = preg_replace('/<(pre)(?:(?!<\/\1).)*?<\/\1>/s','',$page_content);

If you want to include other tags, just add them to the initial matching group like:

(pre|script|style)

the only real issue with regex tag removal is nested tags of the same type, like:

<div>
    <div>inner closing tag might match beginning outer opening div tag leaving an orphan outer closing tag</div>
<div>

Edit

I tested the example you left in the other comment on the other answer, works fine for me:

$html = 'This is a quick snippet that often comes in handy: <pre>[code]blah blah[/code]</pre>';
$html = preg_replace('/<(pre)(?:(?!<\/?\1).)*?<\/\1>/s',"",$html);
var_dump($html);

result:

string(51) "This is a quick snippet that often comes in handy: "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM