I have a html string and needs to remove double quote from href of anchor tag.
$content = '<p style="abc" rel="blah blah"> Hello I am p </p> <a href="https://example.com/abc?name="xyz&123""></a>';
should return
$content = '<p style="abc" rel="blah blah"> Hello I am p </p> <a href="https://example.com/abc?name='xyz&123'"></a>';
I have tried
preg_replace('/<as+[^>]*hrefs*=s*"([^"]+)"[^>]*>/', '<a href="1">', $content)
but this removes all attributes from anchor tag except for href. Unable to find out something that can actually works inside href Looking for some php code for the same.
Advertisement
Answer
You may try:
(<a href=".*?)"(.*?)"(.*)
Explanation of the above regex:
(<a href=".*?)
– Represents first capturing group capturing capturing everything before the first"
. Notice I used lazy matching which facilitates this task."
– Matches"
literally.(.*?)
– Represents second capturing group capturing dataxyz&123
which is in between"
.(.*)
– Represents 3rd capturing group which captures everything after the"
.$1'$2'$3
– For the replacement part; use the captured groups along with single quotes.
You can find the demo of the above regex in here.
Sample Implementation inf php:
<?php $re = '/(<a href=".*?)"(.*?)"(.*)/m'; $str = '<p style="abc" rel="blah blah"> Hello I am p </p> <a href="https://example.com/abc?name="xyz&123""></a>'; $subst = '$1'$2'$3'; $result = preg_replace($re, $subst, $str); echo $result;
You can find the sample run of the above code in here.