PHP code example of scarletsfiction / scarletsquery

1. Go to this page and download the library: Download scarletsfiction/scarletsquery 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/ */

    

scarletsfiction / scarletsquery example snippets


$html = file_get_contents('sample.html');
$dom = Scarlets\Library\MarkupLanguage::parseText($html);

// Select element that have 'name' id and get these content
$dom->selector('#name')->content();
/* Output
    Array
    (
        [0] => Alex
        [1] => Elisabeth
        [2] => Luffy
    )
*/

/*
  1. Select element that have 'ul' tag
  2. Select the child with '3' id
  3. Select the element before it { next(-1) }
  4. Get the content
*/
$dom->selector('ul #3')[0]->next(-1)->content();
/* Output
    Elisabeth
*/

// Select 'ul' tag and the child with 'li' tag
$contents = $dom->selector('ul li');

// Iterate over the result
for ($i=0; $i < $contents->length; $i++) {

    // Check if the element has 'profile' class
    if($contents[$i]->hasClass('profile')){

        // Select the child with 'span' tag
        // And get the second content
        print_r($contents[$i]->selector('span')->content(1));
    }
}
/* Output
    3c
*/