Symfony爬虫获取具有以下同级的文本
给定以下html代码:Symfony爬虫获取具有以下同级的文本,symfony,laravel,xpath,Symfony,Laravel,Xpath,给定以下html代码: <div class="body"> 1. Question <strong>1</strong> <input type="text" /> 2. You have <u>Question</u><strong>1</strong> <input type="text" /> 3. Question <strong&g
<div class="body">
1. Question <strong>1</strong>
<input type="text" />
2. You have <u>Question</u><strong>1</strong>
<input type="text" />
3. Question <strong>3</strong>
<input type="text" />
</div>
它在没有
和
标记的情况下工作正常。到底是谁做的?您可以使用strip_标记($text)从字符串中删除html
在这里读一下
如果要删除起始编号,可以使用explode()(),如
请注意末尾的“2”,这将生成2个元素的数组,第一个元素是数字+点,第二个元素是文本。您可以使用strip_标记($text)从字符串中删除html
在这里读一下
如果要删除起始编号,可以使用explode()(),如
注意末尾的“2”,这将生成2个元素的数组,第一个元素是数字+点,第二个元素是文本
[
0 => 'Question 1', // Or 'Question <strong>1</strong>' is better
1 => 'You have Question 2',
2 => 'Question 3'
]
$results = [];
$questions = $crawler->filterXPath('//*[contains(@class, "body")]/text()[normalize-space()][following-sibling::input]');
$questions = $questions->each(function($c) use (&$results) {
$line = trim($c->text());
if(preg_match('/^[0-9]{1,2}\./', $line, $matches) == true) {
$number = $matches[0];
if(is_numeric($number) && $number != '') {
$results[] = trim(str_replace($number, '', $line));
}
} elseif(!empty($results)) {
$results[count($results) - 1] .= '\n'. $line;
}
});
return $results;
explode(' ', $line, 2)