wikitext / query
一个查询对象,帮助您遍历wikitext。
Requires
- php: ^7.1
- ext-json: *
Requires (Dev)
- phpspec/prophecy: ^1.8
- phpunit/phpunit: ^7.3
This package is auto-updated.
Last update: 2024-09-13 05:24:56 UTC
README
Wikitext Query 是一个PHP查询对象,帮助您遍历wikitext标记语言。
安装
可以使用 Composer 下载并安装Wikitext Query及其依赖项。
composer require wikitext/query
使用
在您开始查询任何内容之前,您必须定义可用的选择器。您可以创建自己的,尽管此包包含一组匹配器。您只需要添加它们
Wikitext\Query::addMatchers([ new Wikitext\Matchers\HeaderMatcher('h1'), // h1, h2, ... new Wikitext\Matchers\UnorderedListMatcher(), // ul new Wikitext\Matchers\OrderedListMatcher(), // ol new Wikitext\Matchers\ListItemMatcher(), // li new Wikitext\Matchers\AnchorMatcher(), // a new Wikitext\Matchers\RowMatcher(), // hr new Wikitext\Matcher('selector', '/regex/') ]);
可以通过传递wikitext内容来创建新的Query对象。
$query = new Wikitext\Query('wikitext');
Query对象将wikitext拆分为单行;或者,您也可以自己传递包含行的数组
$query = new Wikitext\Query(['line 1', 'line 2', 'line 3']);
此初始Query(或主Query)类似于'DOM'。您对它的任何操作或任何子查询都将始终与此原始对象相关。这意味着子选择器仍会引用原始wikitext中存在的行号。
在尝试查询其内容时,查询对象与jQuery有相当多的相似之处。方法和选择器大致相同。每个查询操作都会返回一个新的查询,以允许方法链式调用。
$result = $query->find('h1')->next('hr');
请注意!如next
和prev
之类的查询操作始终关联到初始wikitext。
文档
爬取
查找
返回一个包含所有与选择器匹配的初始wikitext行的Query对象。
Wikitext\Query find(string $selector)
当您在除主对象之外的Query对象上使用find时,它将从中过滤。
下一行
返回一个Query对象,其中包含根据初始wikitext中Query的第一个项之后的行。当指定选择器时,它将返回相应的行。
Wikitext\Query next(string $selector = "*")
如果没有找到任何内容,将返回一个空的Query对象。
上一行
返回一个Query对象,其中包含根据初始wikitext中Query的第一个项之前的行。当指定选择器时,它将返回相应的行。
Wikitext\Query prev(string $selector = "*")
如果没有找到任何内容,将返回一个空的Query对象。
下一行全部
返回一个Query对象,其中包含根据初始wikitext中Query的第一个项之后的行。当指定选择器时,它将返回相应的行。
Wikitext\Query nextAll(string $selector = "*")
如果没有找到任何内容,将返回一个空的Query对象。
上一行全部
返回一个Query对象,其中包含根据初始wikitext中Query的第一个项之前的行。当指定选择器时,它将返回相应的行。
Wikitext\Query prevAll(string $selector = "*")
如果没有找到任何内容,将返回一个空的Query对象。
访问
查询对象中的项可以通过多种方式访问。
获取文本
返回作为单个字符串(用"\n"连接)的Query对象的行。
string getText()
获取项
返回包含Query对象中所有行的数组。数组的键始终对应于它们在初始wikitext中的位置。
array getItems()
是否为空
如果没有行,则返回true。
boolean isEmpty()
计数
返回Query中的行数。
int count()
第一行
返回包含Query的第一行的Query对象。
Wikitext\Query first()
最后一行
返回包含Query的最后一行的Query对象。
Wikitext\Query last()
第n行
返回包含Query的第n行的Query对象。
Wikitext\Query eq(int $index)
匹配
将正则表达式与wikitext进行匹配,并返回定义的索引或默认值。
mixed match(string $pattern, int $index = 0, $default = null)
可计数,ArrayAccess & IteratorAggregate
查询对象可以被视为数组,但总是返回其行作为查询对象。
count($query); // x var_dump($query[3]); // Wikitext\Query foreach ($query as $line) { var_dump($line); // Wikitext\Query }
支持
请在GitHub上此处提交问题