Srim
Srim

Reputation: 547

concate string with href path in php

I have a file with so many href attributes. I want to modify the path, just want to add absolute path with the existing. e.g href="parentall_files/filelist.xml" , just needs to be changed to href="dir/parentall/parentall_files/filelist.xml" throughout the file. I have written the following:

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

But alas! it is not changing the path. Please help.

Upvotes: 1

Views: 151

Answers (4)

asimshahiddIT
asimshahiddIT

Reputation: 330

you can use str_replace.

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

Upvotes: 0

vaso123
vaso123

Reputation: 12391

You need to add the {1} after $ sign

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

Output is:

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

Upvotes: 0

Hmmm
Hmmm

Reputation: 1764

Why you don't just change it with str_replace()

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

Upvotes: 1

Antony D&#39;Andrea
Antony D&#39;Andrea

Reputation: 1004

Try

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

Upvotes: 0

Related Questions