logo
down
shadow

PHP DOMDocument: may a text node be reused?


PHP DOMDocument: may a text node be reused?

By : Mohammad Anwar
Date : November 20 2020, 04:01 AM
To fix the issue you can do A node can only have 1 parent, so as soon as you add it to another node, that is where it will stay.
You can easily clone the node using cloneNode(), but as the name implies - it's not the same node but a copy. If you change the following line to...
code :
$first->appendChild($textNode->cloneNode(true));
<ROOT>
  <FIRST>I should be in both nodes</FIRST>
  <SECOND>I should be in both nodes</SECOND>
</ROOT>


Share : facebook icon twitter icon
How to replace the text of a node using DOMDocument

How to replace the text of a node using DOMDocument


By : reiswindy
Date : March 29 2020, 07:55 AM
help you fix your problem Set DOMNode::$nodeValue instead:
code :
$doc->getElementsByTagName("title")->item(0)->nodeValue = $titleText;
$doc->getElementsByTagName("description")->item(0)->nodeValue = $descriptionText;
$doc->getElementsByTagName("link")->item(0)->nodeValue = $linkText;
How to insert text in the parent node just before a child node in PHP DomDocument

How to insert text in the parent node just before a child node in PHP DomDocument


By : Biswash Bhusal
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I was able to fix the problem. I did a mistake, the code should look like:
code :
if ($key == 0) {
    $squareBracket = $dom_input->createTextNode("[");
    $paragraph->insertBefore($squareBracket, $citation);
}
PHP DomDocument - How to replace a text from a Node with another node

PHP DomDocument - How to replace a text from a Node with another node


By : pooja
Date : March 29 2020, 07:55 AM
hop of those help? Xpath allows you to fetch the text nodes containing the string from the document. Then you have to split it into a list of text and element (xref) nodes and insert that nodes before the text node. Last remove the original text node.
code :
$xml = <<<'XML'
<p>
    This is a test node with Figure 1.
</p>
XML;
$string = 'Figure 1';

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

// find text nodes that contain the string
$nodes = $xpath->evaluate('//text()[contains(., "'.$string.'")]');
foreach ($nodes as $node) {
  // explode the text at the string
  $parts = explode($string, $node->nodeValue);
  // add a new text node with the first part
  $node->parentNode->insertBefore(
    $dom->createTextNode(
      // fetch and remove the first part from the list
      array_shift($parts)
    ),
    $node
  );
  // if here are more then one part
  foreach ($parts as $part) {
    // add a xref before it
    $node->parentNode->insertBefore(
      $xref = $dom->createElement('xref'),
      $node
    );
    // with the string that we used to split the text
    $xref->appendChild($dom->createTextNode($string));
    // add the part from the list as new text node
    $node->parentNode->insertBefore(
      $dom->createTextNode($part), 
      $node
    );
  }
  // remove the old text node
  $node->parentNode->removeChild($node);
} 

echo $dom->saveXml($dom->documentElement);
<p>
    This is a test node with <xref>Figure 1</xref>.
</p>
php DomDocument -- find empty text node?

php DomDocument -- find empty text node?


By : user2931993
Date : March 29 2020, 07:55 AM
will help you It's not telling you there is an empty text node there because there isn't a text node there - that simple. The xpath expression to pick out that book would be:
code :
/books/book[not(sep/preceding-sibling::text())]
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>

<books>
    <book>first title<sep/>first author</book>
    <book>second title<sep/>second author</book>
    <book>third title<sep/>third author</book>
    <book><sep/>fourth author</book>
</books>
XML;

$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);

$query = '/books/book[not(sep/preceding-sibling::text())]';
foreach ($xpath->query($query) as $book) {
    $title = $dom->createTextNode("fourth title");
    $book->insertBefore($title, $book->firstChild);
    echo $dom->saveXML($book), "\n";
}
<book>fourth title<sep/>fourth author</book>
Replace DOMDocument Parent node with child node PHP

Replace DOMDocument Parent node with child node PHP


By : Rishab Gautam
Date : March 29 2020, 07:55 AM
will help you Hello_ mate.
If I understood you well you want to remove the tags and I don't know what exactly your function cj_DOMinnerHTML is doing, but I see that your are passing instance of DOMDocument to replaceChild method as first argument which is wrong. Refer to documentation to see how exactly is replaceChild working (it accepts two arguments of type DOMNode). Anyway I give you a code snippet that is replacing the tags. Please read the comments that I put in code and try to change the code for your use case.
code :
$html = '
<div id="container">
    <a href="xxx.com"><img src="yyy.jpg"></a>
    <a href="aaa.com"><img src="aaa.jpg"></a>
    <a href="bbb.com"><img src="bbb.jpg"></a>
    <a href="ccc.com"><img src="ccc.jpg"></a>
    <a href="ddd.com"><img src="ddd.jpg"></a>
    <a href="eee.com"><img src="eee.jpg"></a>
</div>';

// load the dom document
$dom = new \DOMDocument();
if (!$dom->loadHTML($html)) {
    echo '<h2>Error handle this ...</h2>';
}

// instantiate DOMXPath object
$finder = new \DOMXPath($dom);

// get all <a> tags of element that has id="container"
$anchors = $finder->query("//*[contains(concat(' ', normalize-space(@id), ' '), 'container')]/descendant::a");

// loop through all <a>
foreach ($anchors as $a) {
    $parent = $a->parentNode;
    // the following row of code will actually remove the <a> tag
    $parent->replaceChild($a->childNodes->item(0), $a);
}

// show output
echo htmlspecialchars($dom->saveHTML());
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> 
<html>
    <body>
        <div id="container"> 
            <img src="yyy.jpg"> 
            <img src="aaa.jpg"> 
            <img src="bbb.jpg"> 
            <img src="ccc.jpg"> 
            <img src="ddd.jpg"> 
            <img src="eee.jpg"> 
        </div>
    </body>
</html> 
Related Posts Related Posts :
  • change the link color if a post has been read wordpress
  • How do you customize variables in Laravel default emails?
  • Extract content of specific div class php dom
  • Showing results of a database query inside Navigation Tab
  • Return many object in my json response using resource
  • How to upload an image to Azure storage through PHP?
  • PHP - DOMDocument scrap divs dont remove images
  • Adding to functions.php in child theme doesn't work
  • Regular Expression to match markdown and regular href sources from specific domain(s)
  • How to manipulate and collate data in an associate array
  • How do I check an array for an entry that contains HTML characters?
  • Select current ( week or any given week ) records excluding week ends and holidays
  • How do I find the shipping class id in WooCommerce?
  • Make a select field disabled with a preselected value in Woocommerce
  • How to use the querybuilder in doctrine to test whether or not atleast one of two given values is already set in the dat
  • Laravel File Privacy
  • PHP read TXT file from a special character
  • Select first 10 rows and display into a slideshow
  • PHPMailer autoload.php not able to open
  • PHP - Create GUID from ID
  • PHP variable causing my SQL query to fail, but hard-coding the same values into the query works Why is this?.
  • generate a random password and update the database
  • DELETE to /oauth/personal-access-tokens/token not supported
  • Forget not modifying the original collection Laravel 5.7
  • Print a sequence of letters
  • Is there any alternative for asset and URL in Laravel?
  • PHP - how to link other pages in view in ci
  • Cannot Access Laravel Storage Directory
  • Find and replace value in system.cfg php
  • Laravel : How to retrieve data from inside an nested array
  • Codeigniter search with comma-separated values
  • upload file on laravel 5.7
  • Wordpress - How to display 10 users only?
  • Display the results in the HTML
  • PHP - mysqli_fetch_assoc, 2 results then into an array
  • mysqli update database receiving email and password reset token
  • PHP mailer not creating new lines
  • IF / OR statement causes Call to undefined function() error. Why?
  • How to read Protocol Buffers delimited I/O functions
  • Creating a link from Google Chart timeline item
  • Rewrite PHP variables from URL with .htaccess
  • Undesired output for php array creation
  • Symfony 4 - custom authentication FosUserBundle
  • laravel: cant push object property to array
  • When sharing on facebook, showing angular brackets in title and decription
  • Undefined function: 7 ERROR: operator does not exist: public.ltree public.< public.ltree[]
  • Generate a CSV in php from Array or Object
  • LibreOffice convert PDF to Word as textbox instead normal document
  • php laravel swift email sending problems
  • Reload PHP file in command line PHP-CLI
  • how do I fetch a certain part out of an database?
  • CodeIgniter URL Route with ID and title
  • Add a header to each column
  • How can I loop stdClass in PHP?
  • Four arrays in for each loop
  • bootstrap 4 cards with php foreach loop
  • Codeigniter return not found on the server
  • PHP Codeigniter Do Upload Function Does Not Work
  • Implement REST API Symfony 3
  • how to add show data button in Yajrabox
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org