如何使用regex php替换结果URL
我想使用简单的dom解析web,我得到如下输出 我的代码如何使用regex php替换结果URL,php,Php,我想使用简单的dom解析web,我得到如下输出 我的代码 foreach($html->find('ul[class=result-topic] a ') as $e){ $count++; $d = ($e->href )."<br>"; echo($d)."<br>"; if($count == 2) { exit(); } } 这不是完整的文章,如何用完整的文章替换url,像这里一样,输出 /news/full/3549858
foreach($html->find('ul[class=result-topic] a ') as $e){
$count++;
$d = ($e->href )."<br>";
echo($d)."<br>";
if($count == 2)
{
exit();
}
}
这不是完整的文章,如何用完整的文章替换url,像这里一样,输出
/news/full/354985850
/film/full/74808409409
一种方法是
分解
,将第二个元素附加到最后一个元素,然后内爆
$string = '/news/354985850';
$parts = explode('/', $string);
$parts[(count($parts) - 1)] = 'full/' . $parts[(count($parts) - 1)];
echo implode('/', $parts);
演示:
另一种方法是带有preg\u replace
的正则表达式:
$string = '/news/354985850';
echo preg_replace('~(.*)/~', '$1/full/', $string);
演示:
Regex演示:如何在类别(新闻、电影等)之后、帖子id(3549850748409409等)之前插入“完整”如何使用内爆?
$string = '/news/354985850';
echo preg_replace('~(.*)/~', '$1/full/', $string);