Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用regex php替换结果URL_Php - Fatal编程技术网

如何使用regex php替换结果URL

如何使用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

我想使用简单的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/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);