-2

I would like to change all href's in a website.

<a href="sitename">xyz …</a>

to:

<a href="sitename.html>xyz …</a>

Thanks for help!

chris85
  • 23,846
  • 7
  • 34
  • 51
Harold
  • 1
  • 1
  • Use a parser. Do not use a regex (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#answer-1732454). As you see in your own question, with the missing closing quote, you don't know what you're going to find in that HTML. – LSerni Mar 07 '17 at 22:38

2 Answers2

0

You could do it using the following way ...

$hrefs = '<a href="sitename1">xyz …</a>
<a href="sitename2">xyz …</a>';
$r = '/(?<=href=").*?(?=">)/';
$sitename = 'sitename.html';
$result = preg_replace($r, $sitename, $hrefs);
echo $result;

DEMO

m87
  • 4,445
  • 3
  • 16
  • 31
-1
$content = file_get_contents('page.html');

$new_content = str_replace('<a href="', '<a href="sitename.html', $content);

echo $new_content;
clearshot66
  • 2,292
  • 1
  • 8
  • 17