Is this a bug? Anyone have any idea?
$var = '9gag.com/gag/144972/';
$test1 = '<a href="http://9gag.com/gag/144972/" target="_blank"></a>';
$test1 = preg_replace('#(<a href="(\\w+://)?([' . preg_quote( $var ) . ']+)"[^>]+>)(</a>)#', "\\\\1$var\\\\4", $test1 );
echo $test1;
If the first character is a digit, preg_replace seems to always removes it!
put a space between the \\1 and the $var.
I see, thanks a million StarLion!
This is already covered in the manual, which Iâm going to assume youâve read.
When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \\1 notation for your backreference. \\11, for example, would confuse preg_replace() since it does not know whether you want the \\1 backreference followed by a literal 1, or the \\11 backreference followed by nothing. In this case the solution is to use \${1}1. This creates an isolated $1 backreference, leaving the 1 as a literal.
In your case, the replacement string is sent to preg_replace() as "\\19gag.com/gag/144972/\\4"
, which it reads as âbackreference 19, literal gag.com/gag/144972/ then backreference 4.â The solution, as the docs say, would be to make your string like "${1}$var$4"
.
Salathe:
This is already covered in the manual, which Iâm going to assume youâve read.
In your case, the replacement string is sent to preg_replace() as "\\19gag.com/gag/144972/\\4"
, which it reads as âbackreference 19, literal gag.com/gag/144972/ then backreference 4.â The solution, as the docs say, would be to make your string like "${1}$var$4"
.
THanks Salathe, I did read the part youâve highlighted on the manual but I put it the wrong position like so:
\\1{$var}\\4
Lol, dumb me but thanks for your input. Got it working! \${1}