PHP code example of gymadarasz / xparser

1. Go to this page and download the library: Download gymadarasz/xparser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gymadarasz / xparser example snippets



oad a DOM root form a string or file/url
$x = new gymadarasz\xparser\XNode(file_get_contents('http://your-important-document-or-template.com'));

// select elements via simple CSS selector and read attributs or manipulate contents easily e.g.:
$x('a#your_link')->inner('Hello world')->href = 'http://your.hellopage.com';

// or make a foreach on all elements

foreach($x('div.hello') as $index => $hellodiv) {
  $hellodiv->inner('It is the ' . $index . 'th Hello DIV!');
}

// or just use jQuery style 'each' function, it's same

$x('#nav a', function($elem) {
  $elem->href = '//myurl/' . $elem->href;
});

// show document
echo $x;

if($x->validate()) {
  // ..do something here
  $x->find('.hello2')->outer();
}
else {
  // your html contains an invalid closure structure
  die('Invalid document!');
}

// it will be return how many <div> element are in your xhtml
$count = $xnode->getCount('div'); 

$span = $x->find('span', 0);
$parent = $span->getParent();


// make a DOM node element
$x = new XNode('<p>Your XHTML or XML here...</p>');

// get any attribute
$attr = $x->attr('href');

// same but shorten:
$attr = $x->href;

// set any attributes
$x->attr('href', 'link-here');

// or just:
$x->href = 'link-here';

// find elements in the dom struct, e.g. find all <div> elements:
$x->find('div');
// or find an n-th element only:
$x->find('div', 3);

// or sort format:
$x('div');

// here, you can use complex css selection e.g:
$x('div#first, div#sixth, div.selected')

// or you can use typically method calls
$x->getElementById('element01');
$x->getElements($tagName, $attributeRegex, $attributeValueRegex); // <- all parameters are optional
$x->getElementsByClass('class-name');

// get/set element inner text:
$inner = $x->inner();
$x->inner('Hello World!');

// get/set element outer text:
$outer = $x->outer();
$x->outer('It\'ll replace the element!');