PHP preg_quote in replacement string

I am using preg_replace and I want to include the url in the replacement string. How do I quote this line? It looks like preg_quote is for the search pattern only.

$replace = '\1'.addslashes($url).'\3'.addslashes($title).'\4';
+5
source share
5 answers
  • Screens needed
  • addslashes not enough
  • preg_quote avoids too much

Watch this demo .

As Mario noted, you can use addcslashes($str, "\\$").

+4
source

Unfortunately, there is no general way to do this, but addslashesshould be sufficient in most cases.

For maximum security, you can use the syntax ${1}. For instance.

$replace = '${1}'.addslashes($url).'${3}'.addslashes($title).'${4}';

, preg_replace_callback(). , , -, .

preg_replace_callback():

class URLReplacer {
    public $pattern = '/my regex/';
    public $url;
    public $title;
    function __construct($url, $title) {
        $this->url = $url;
        $this->title = $title;
    }
    function _callback($matches) {
        return $matches[1].$url.$matches[3].$title.$matches[4];
    }
    function replace($subject) {
        return preg_replace_callback($this->pattern, array($this, '_callback'), $subject);
    }
}
$repl = new URLReplacer($url, $title);
$replaced = $repl->replace($subject);
0

, . , , :

$url = 'http://example.com/';
$title = 'Make it Complex \4';

$subject = 'Call \\4 " $me an url';
$pattern = '/(.*)an()( )(url)/';

$replace = function($m) use ($url, $title)
{
    return "$m[1]$url$m[3]$title$m[4]";
};

$result = preg_replace_callback($pattern, $replace, $subject);

:

Call \4 " $me http://example.com/ Make it Complex \4url

Docs, .

, , , . . {\1} 1, {$2} . :

$patternf = function()
{
    $values = func_get_args();
    $mask = $values ? array_shift($values) : NULL;
    return function($matches) use ($mask, $values)
    {
        $parts = preg_split('/({[\\\\\\$][0-9]{1,3}})/', $mask, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        foreach($parts as &$part)
            if (preg_match('/^{([\\\\\\$])([0-9]{1,3})}$/', $part, $p))             
                $part = $p[1] == '\\' ? $matches[(int)$p[2]] : $values[$p[2]-1];
        return implode('', $parts);
    };
};

:

$replace = $patternf('{\1}{$1}{\3}{$2}{\4}', $url, $title);

$result = preg_replace_callback($pattern, $replace, $subject);

. :

function preg_replace_subst($pattern, $replace, $subject)
{
    $values = func_get_args();
    $pattern = array_shift($values);
    $mask = array_shift($values);
    $subject = array_shift($values);
    $callback = function($matches) use ($mask, $values)
    {
        $parts = preg_split('/({[\\\\\\$][0-9]{1,3}})/', $mask, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        foreach($parts as &$part)
            if (preg_match('/^{([\\\\\\$])([0-9]{1,3})}$/', $part, $p))             
                $part = $p[1] == '\\' ? $matches[(int)$p[2]] : $values[$p[2]-1];
        return implode('', $parts);
    };
    return preg_replace_callback($pattern, $callback, $subject);
}

:

$url = 'http://example.com/';
$title = 'Make it Complex \4';

$subject = 'Call \\4 " $me an url';
$pattern = '/(.*)an()( )(url)/';

$replace = '{\1}{$1}{\3}{$2}{\4}';

$result = preg_replace_subst($pattern, $replace, $subject, $url, $title);

, .

e preg_replace ( )

e replace . , PHP, :

$url = 'http://example.com/';
$title = 'Make it Complex \4';

$subject = 'Call me an url.';

$pattern = '/(.*)an()( )(url)/e';
$replace = '"$1{$url}$3{$title}$4"';

$result = preg_replace($pattern, $replace, $subject);

:

Call me http://example.com/ Make it Complex \4url.

, e -modifier , $ $subject, PHP . . , , :

$url = 'http://example.com/';
$title = 'Make it Complex \4';

$subject = 'Call \\4 " $me an url';

$pattern = '/(.*)an()( )(url)/e';
$replace = "'\$1'.\$url.'\$3'.\$title.'$4'";

:

Call \4 \" $me http://example.com/ Make it Complex \4url
        ^ problem, not in input.

, , .

0

, $replacement preg_replace(), :

function preg_quote_replacement($input) {
    return addcslashes($input, '\\$');
}

In the case of OP:

$subject = preg_replace(
    $pattern,
    '\1'.preg_quote_replacement($url).'\3'.preg_quote_replacement($title).'\4',
     $subject
);
0
source

You can use the T-Regx library, which has a Pattern Builder . It works like prepared statements in SQL:

Pattern::inject("\1@url\2@url\3", [
    'url' => $input
]);

or even

Pattern::prepare("\1", [$input], "\2", [$input], "\3");
               //      ↑ this means 'ignoring special characters'
0
source

All Articles