使用REGEX替换单引号与域周围的双引号

I need to replace all instances of single quotes surrounding my website. I would like to replace the single quotes with double quotes. I know a REGEX should do the trick, but I'm not sure how to structure it. I'd like it to affect any single quotes that contain a string that starts with http://www.website.com.

Basically, I'd like to be able to run a find and replace and have code that currently looks like this:

<img src='http://www.website.com/images/icons/main_icon.png' width='100' scalefit='1'>
<a href='http://www.website.com/page.php?variable1=a&variable2=b'>Click Here</a>
<form action='http://www.website.com/action.php?action=$action_type' method='post'>

Turn into something like this:

<img src="http://www.website.com/images/icons/main_icon.png" width='100' scalefit='1'>
<a href="http://www.website.com/page.php?variable1=a&variable2=b">Click Here</a>
<form action="http://www.website.com/action.php?action=$action_type" method='post'>

PHP's DOMDocument class does this automatically for you. All you need to do is load the HTML and save it back. This might work better than a regex as it's not dependent on the markup format. The downside is that it will fix all the HTML, not just the quotes around attributes that begin with a particular domain.

$dom = new DOMDocument;
libxml_use_internal_errors(TRUE);
$dom->loadHTML($html);
$result = $dom->saveHTML();

Demo


I don't see a use-case for replacing just some particular piece of HTML markup. But I don't know what you're trying to accomplish, so I will offer a regex solution as well:

preg_replace("#\b\w+\s*='((?=http://www\.website\.com)[^']+)'#is",'"$1"',$html);

This uses a positive lookahead to check of the attribute value begins with with the URL you've defined.

Regex101 demo

You can match the following regex:

=\'(http:\/\/www\.website\.com.+?)\'

and replace with:

="\1"

Demo: http://regex101.com/r/wQ7jV5

I am assuming you only want to match if the prefix is http://www.website.com.

Note that we use ! as a regex delimiter in the expression below, so we do not have to escape the / inside the expression.

<?php
$input="<img src='http://www.website.com/images/icons/main_icon.png' width='100' scalefit='1'>";
$output = preg_replace("!src='(http://www\.website\.com[^']+)'!", 'src="\\1"', $input);
echo $output."
";
?>