在 PHP 中使用 href 路径连接字符串


concate string with href path in php

我有一个文件,其中包含如此多的href属性。我想修改路径,只想用现有的路径添加绝对路径。例如href="parentall_files/filelist.xml",只需要在整个文件中更改为href="dir/parentall/parentall_files/filelist.xml"即可。我写了以下内容:

 $contents = preg_replace('/<a href="(.*?)"/i', '<a href="dir'/parentall'/$"',$contents);

但唉!它并没有改变道路。请帮忙。

为什么

你不用str_replace()来改变它

$contents = str_replace('href="parentall_files/','href="dir/parentall/parentall_files/', $contents);

尝试

preg_replace('/<a href="(.*?)"/', '<a href="dir'/parentall'/"',$contents)
您需要在

符号后添加$ {1}

$string = '<a href="parentall_files/filelist.xml">asdsadsad</a>';
$new = preg_replace('/<a href="(.*?)"/i', '<a href="dir/parentall/${1}', $string);

输出为:

string '<a href="dir/parentall/parentall_files/filelist.xml>asdsadsad</a>' (length=65)

您可以使用str_replace .

$contents= str_replace('href="','href="dir/parentall/',$contents);