avenger_msoft
Новичок
Помогимте оптимизировать функции (preg_replace_callback)...
Привет Всем!
Мне необходимо привести все URL на сайте типа:
http://avenger/index.php?module=news
http://avenger/index.php?module=projects&action=show
http://avenger/index.php?module=projects&action=show&pid=1&id=236&page=1
к виду:
http://avenger/news/index.html
http://avenger/projects/show/index.html
http://avenger/projects/show/1/236/index.html?page=1
Общий вид преобразования можно задать так:
http://avenger/$module/$action/$pid/$id/index.html?#ОСТАЛЬНЫЕ ПАРАМЕТРЫ#
Я написал соответствующие функции:
Надо оптимизоровать функцию __callback_Rebuild_URL, и по возможности регулярное выражение "# (['\"]{1}) " . "index\.php" . "(?:\?) ([^<>]*) ?\\1{1}? #ixU"
С уважением, Иван.
Привет Всем!
Мне необходимо привести все URL на сайте типа:
http://avenger/index.php?module=news
http://avenger/index.php?module=projects&action=show
http://avenger/index.php?module=projects&action=show&pid=1&id=236&page=1
к виду:
http://avenger/news/index.html
http://avenger/projects/show/index.html
http://avenger/projects/show/1/236/index.html?page=1
Общий вид преобразования можно задать так:
http://avenger/$module/$action/$pid/$id/index.html?#ОСТАЛЬНЫЕ ПАРАМЕТРЫ#
Я написал соответствующие функции:
PHP:
function __callback_Rebuild_URL($__matches) {
$__vars = array();
$__result = preg_replace('@(\w+)=(\w+)@e', '$__vars["\\1"] = "\\2";', $__matches[2]);
$__keys = array('module', 'action', 'pid', 'id');
$URL_Path = "";
reset($__keys);
while (count($__vars) && list(, $key_id) = each($__keys)) {
if (!isset($__vars[$key_id]))
break;
$URL_Path .= "/" . $__vars[$key_id];
unset($__vars[$key_id]);
}
$URL_Parameters = "";
reset($__vars);
foreach ($__vars as $name => $value) {
$URL_Parameters .= $name . "=" . $value . "&";
}
$URL_Parameters = preg_match("#&$#", $URL_Parameters) ?
substr($URL_Parameters, 0, -1) : $URL_Parameters;
if (count($__vars))
$URL_Parameters = "?" . $URL_Parameters;
return $__matches[1] . $URL_Path . "/index.html" . $URL_Parameters . $__matches[1];
}
function smarty_outputfilter_Communicator_Rewrite_URL($source, &$smarty) {
$source = preg_replace("@&(amp|#38);@i", "&", $source);
$source = preg_replace_callback(
"# (['\"]{1}) " . "index\.php" . "(?:\?) ([^<>]*) ?\\1{1}? #ixU",
"__callback_Rebuild_URL", $source);
return $source;
}
С уважением, Иван.