: PHP, . .
, .
( ) preg_replace_callback(), @mario, e, preg_replace() PHP-:
<?php
$shortCodes = array (
'r' => 'ref',
't' => 'type'
);
$expr = '#\{([a-z])\|([^|]+)\|([^}]+)\}#e';
$replace = '"<a href=\"/{$shortCodes[\'$1\']}/$2\">$3</a>"';
$string = 'Some text as a ref {r|link1.php|link} and a type {r|link2.php|link}';
echo preg_replace($expr, $replace, $string);
, , , LinkTitle , \' .
,
, , , , urlencode()/htmlspecialchars(), :
<?php
$shortCodes = array (
'r' => 'ref',
't' => 'type'
);
$expr = array(
'#\{([a-z])\|([^|]+)\|([^}]*"[^}]*)\}#e',
'#\{([a-z])\|([^|]+)\|([^}]+)\}#e'
);
$replace = array(
'"<a href=\"/{$shortCodes[\'$1\']}/".htmlspecialchars(urlencode(\'$2\'))."\">".htmlspecialchars(str_replace(\'\"\', \'"\', \'$3\'))."</a>"',
'"<a href=\"/{$shortCodes[\'$1\']}/".htmlspecialchars(urlencode(\'$2\'))."\">".htmlspecialchars(\'$3\')."</a>"'
);
$string = 'Some text as a ref {r|link &1.php|link&\' with some bad characters in it} and a type {r|link2.php|link with some "quotes" in it}';
echo preg_replace($expr, $replace, $string);
:
Some text as a ref <a href="/ref/link+%261.php">link&' with some bad characters in it</a> and a type <a href="/ref/link2.php">link with some "quotes" in it</a>
,