Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <p>The quick <a href="/">brown</a> fox jumps <!--more-->
- over the <a href="/">lazy</a> dog.</p>
- <p>The quick <a href="/">brown</a> fox jumps FOOBAR
- array(2) {
- [0]=>
- string(50) "<p>The quick <a href="/">brown</a> fox jumps "
- [1]=>
- string(45) " over the <a href="/">lazy</a> dog.</p>"
- }
- <?php
- $reader = new XMLReader;
- $writer = new XMLWriter;
- // load the XML string into the XMLReader
- $reader->xml('<p>The quick <a href="/">brown</a> fox jumps <!--more--> over the <a href="/">lazy</a> dog.</p>');
- // write the new XML to memory
- $writer->openMemory();
- $done = false;
- // XMLReader::read() moves the current read location to the next node
- while ( !$done && $reader->read()) {
- // choose action based on the node type
- switch ($reader->nodeType) {
- case XMLReader::ELEMENT:
- // read an element, so write it back to the output
- $writer->startElement($reader->name);
- if ($reader->hasAttributes) {
- // loop through all attributes and write them
- while($reader->moveToNextAttribute()) {
- $writer->writeAttribute($reader->name, $reader->value);
- }
- // move back to the beginning of the element
- $reader->moveToElement();
- }
- // if the tag is empty, close it now
- if ($reader->isEmptyElement) {
- $writer->endElement();
- }
- break;
- case XMLReader::END_ELEMENT:
- $writer->endElement();
- break;
- case XMLReader::TEXT:
- $writer->text($reader->value);
- break;
- case XMLReader::COMMENT:
- // you can change this to be more flexible if you need
- // e.g. preg_match, trim, etc.
- if (trim($reader->value) == 'more') {
- // write whatever you want in here. If you have xml text
- // you want to write verbatim, use writeRaw() instead of text()
- $writer->text('FOOBAR');
- // this is where the magic happens -- endDocument closes
- // any remaining open tags
- $writer->endDocument();
- // stop the loop (could use "break 2", but that gets confusing
- $done = true;
- }
- break;
- }
- }
- echo $writer->outputMemory();
- str_replace('<!--more-->', 'FOOBAR', $original_text);
- function force_balance_tags( $text ) {
- $bad_text = "<div> <p> some text </p> " ;
- $h = '<p>The quick <a href="/">brown</a> fox jumps <!--more-->
- over the <a href="/">lazy</a> dog.</p>';
- $parts = explode("<!--more-->", $h, 2);
- $front = $parts[0];
- /* Find all opened tags in the front string */
- $tags = array();
- preg_match_all("|<([a-z][w]*)(?: +w*="[\w/%&=]+")*>|i", $front, $tags, PREG_OFFSET_CAPTURE);
- array_shift($tags); /* get rid of the complete match from preg_match_all */
- /* Check if the opened arrays have been closed in the front string */
- $unclosed = array();
- foreach($tags as $t) {
- list($tag, $pos) = $t[0];
- if(strpos($front, "</".$tag, $pos) == false) {
- $unclosed[] = $tag;
- }
- }
- /* Print the start, the replacement, and then close any open tags. */
- echo $front;
- echo "FOOBAR";
- foreach($unclosed as $tag) {
- echo "</".$tag.">";
- }
- <p>The quick <a href="/">brown</a> fox jumps FOOBAR</p>
Add Comment
Please, Sign In to add comment