Guest User

Untitled

a guest
Apr 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. <p>The quick <a href="/">brown</a> fox jumps <!--more-->
  2. over the <a href="/">lazy</a> dog.</p>
  3.  
  4. <p>The quick <a href="/">brown</a> fox jumps FOOBAR
  5.  
  6. array(2) {
  7. [0]=>
  8. string(50) "<p>The quick <a href="/">brown</a> fox jumps "
  9. [1]=>
  10. string(45) " over the <a href="/">lazy</a> dog.</p>"
  11. }
  12.  
  13. <?php
  14. $reader = new XMLReader;
  15. $writer = new XMLWriter;
  16.  
  17. // load the XML string into the XMLReader
  18. $reader->xml('<p>The quick <a href="/">brown</a> fox jumps <!--more--> over the <a href="/">lazy</a> dog.</p>');
  19. // write the new XML to memory
  20. $writer->openMemory();
  21. $done = false;
  22.  
  23. // XMLReader::read() moves the current read location to the next node
  24. while ( !$done && $reader->read()) {
  25. // choose action based on the node type
  26. switch ($reader->nodeType) {
  27. case XMLReader::ELEMENT:
  28. // read an element, so write it back to the output
  29. $writer->startElement($reader->name);
  30. if ($reader->hasAttributes) {
  31. // loop through all attributes and write them
  32. while($reader->moveToNextAttribute()) {
  33. $writer->writeAttribute($reader->name, $reader->value);
  34. }
  35. // move back to the beginning of the element
  36. $reader->moveToElement();
  37. }
  38. // if the tag is empty, close it now
  39. if ($reader->isEmptyElement) {
  40. $writer->endElement();
  41. }
  42. break;
  43. case XMLReader::END_ELEMENT:
  44. $writer->endElement();
  45. break;
  46. case XMLReader::TEXT:
  47. $writer->text($reader->value);
  48. break;
  49. case XMLReader::COMMENT:
  50. // you can change this to be more flexible if you need
  51. // e.g. preg_match, trim, etc.
  52. if (trim($reader->value) == 'more') {
  53.  
  54. // write whatever you want in here. If you have xml text
  55. // you want to write verbatim, use writeRaw() instead of text()
  56. $writer->text('FOOBAR');
  57.  
  58. // this is where the magic happens -- endDocument closes
  59. // any remaining open tags
  60. $writer->endDocument();
  61. // stop the loop (could use "break 2", but that gets confusing
  62. $done = true;
  63. }
  64. break;
  65. }
  66. }
  67. echo $writer->outputMemory();
  68.  
  69. str_replace('<!--more-->', 'FOOBAR', $original_text);
  70.  
  71. function force_balance_tags( $text ) {
  72.  
  73. $bad_text = "<div> <p> some text </p> " ;
  74.  
  75. $h = '<p>The quick <a href="/">brown</a> fox jumps <!--more-->
  76. over the <a href="/">lazy</a> dog.</p>';
  77.  
  78. $parts = explode("<!--more-->", $h, 2);
  79. $front = $parts[0];
  80.  
  81. /* Find all opened tags in the front string */
  82. $tags = array();
  83. preg_match_all("|<([a-z][w]*)(?: +w*="[\w/%&=]+")*>|i", $front, $tags, PREG_OFFSET_CAPTURE);
  84. array_shift($tags); /* get rid of the complete match from preg_match_all */
  85.  
  86. /* Check if the opened arrays have been closed in the front string */
  87. $unclosed = array();
  88. foreach($tags as $t) {
  89. list($tag, $pos) = $t[0];
  90. if(strpos($front, "</".$tag, $pos) == false) {
  91. $unclosed[] = $tag;
  92. }
  93. }
  94.  
  95. /* Print the start, the replacement, and then close any open tags. */
  96. echo $front;
  97. echo "FOOBAR";
  98. foreach($unclosed as $tag) {
  99. echo "</".$tag.">";
  100. }
  101.  
  102. <p>The quick <a href="/">brown</a> fox jumps FOOBAR</p>
Add Comment
Please, Sign In to add comment