PHP code example of bdk / css-xpath

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

    

bdk / css-xpath example snippets


\bdk\CssXpath\CssXpath::cssToXpath('ul > li:first-child');	// returns '//ul/li[1]'

$html = <<<HTML
<div id="article" class="block large">
  <h2>Article Name</h2>
  <p>Contents of article</p>
  <ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
    <li><a href="#">Five</a></li>
  </ul>
</div>
HTML;

// use static method
var_dump(\bdk\CssXpath\CssSelect::select($html, 'ul > li:last-child [href]'));

// or create and use an instance
$cssSelect = new \bdk\CssXpath\CssSelect($html);
$found = $cssSelect->select('ul > li:last-child [href]');
text
array (size=1)
  0 =>
    array (size=3)
      'name' => string 'a' (length=1)
      'attributes' =>
        array (size=1)
          'href' => string '#' (length=1)
      'innerHTML' => string 'Five' (length=4)