内容过滤


QueryList自带内容过滤功能:

详情:Query()主方法

  1. <?php
  2. require 'QueryList/vendor/autoload.php';
  3. use QL\QueryList;
  4. $html =<<<STR
  5. <div id="demo">
  6. xxx
  7. <span class="tt">yyy</span>
  8. <span>zzz</span>
  9. <p>nnn</p>
  10. </div>
  11. STR;
  12. //只想获取内容:xxx
  13. $data = QueryList::Query($html,array(
  14. 'txt' => array('#demo','text','-span -p')
  15. ))->data;
  16. print_r($data);
  17. /**
  18. 结果:
  19. Array
  20. (
  21. [0] => Array
  22. (
  23. [txt] => xxx
  24. )
  25. )
  26. **/
  27. //去掉p标签,但保留p标签的内容
  28. $data = QueryList::Query($html,array(
  29. 'txt' => array('#demo','html','p')
  30. ))->data;
  31. print_r($data);
  32. /**
  33. 结果:
  34. Array
  35. (
  36. [0] => Array
  37. (
  38. [txt] => xxx
  39. <span class="tt">yyy</span>
  40. <span>zzz</span>
  41. nnn
  42. )
  43. )
  44. **/
  45. //获取纯文本,但保留p标签
  46. $data = QueryList::Query($html,array(
  47. 'txt' => array('#demo','text','p')
  48. ))->data;
  49. print_r($data);
  50. /**
  51. 结果:
  52. Array
  53. (
  54. [0] => Array
  55. (
  56. [txt] => xxx
  57. yyy
  58. zzz
  59. <p>nnn</p>
  60. )
  61. )
  62. */
  63. //去掉class名为tt的元素和p标签,但保留p标签的内容
  64. $data = QueryList::Query($html,array(
  65. 'txt' => array('#demo','html','-.tt p')
  66. ))->data;
  67. print_r($data);
  68. /**
  69. 结果:
  70. Array
  71. (
  72. [0] => Array
  73. (
  74. [txt] => xxx
  75. <span>zzz</span>
  76. nnn
  77. )
  78. )
  79. */