我想用简单的dom解析web,我得到的输出像这里
my code
foreach($html->find('ul[class=result-topic] a ') as $e){
$count++;
$d = ($e->href )."<br>";
echo($d)."<br>";
if($count == 2)
{
exit();
}
}
输出/news/354985850
/film/74808409409
这不是完整的文章,如何将url替换为完整的文章,如这里,输出
/news/full/354985850
/film/full/74808409409
一种方法是将explode
附加到第二个元素的最后一个,然后是implode
。
$string = '/news/354985850';
$parts = explode('/', $string);
$parts[(count($parts) - 1)] = 'full/' . $parts[(count($parts) - 1)];
echo implode('/', $parts);
演示:https://eval.in/669122
另一种方法是使用preg_replace
:
$string = '/news/354985850';
echo preg_replace('~(.*)/~', '$1/full/', $string);
演示:https://eval.in/669123
Regex Demo: https://regex101.com/r/dQJY8v/1