php中带有href路径的concate字符串

php中带有href路径的concate字符串,php,html,Php,Html,我有一个文件有这么多href属性。我想修改路径,只想添加绝对路径与现有路径。 e、 ghref=“parentall\u files/filelist.xml”,只需在整个文件中更改为href=“dir/parentall/parentall\u files/filelist.xml”。 我写了以下内容: $contents = preg_replace('/<a href="(.*?)"/i', '<a href="dir\/parentall\/$"',$contents);

我有一个文件有这么多href属性。我想修改路径,只想添加绝对路径与现有路径。 e、 g
href=“parentall\u files/filelist.xml”
,只需在整个文件中更改为
href=“dir/parentall/parentall\u files/filelist.xml”
。 我写了以下内容:

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

preg_replace('/您可以使用


为什么不直接用
str\u replace()


您需要在
$
符号之后添加
{1}

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

Hi codeingsane,我试过了,但我认为我犯了一些愚蠢的错误。首先,我使用了file_get_contents(“html_file_path_here”);然后替换href属性的路径,最后回显内容。是吗?非常感谢。它正在工作。实际上,我犯了一个错误,我应该替换href和src
$contents= str_replace('href="','href="dir/parentall/',$contents);
$contents = str_replace('href="parentall_files/','href="dir/parentall/parentall_files/', $contents);
$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)