Regular expression transcription
I have to translate this: http://*myurl*/feedback.php?rcode=1307954819&lang=it
in
http://*myurl*/index.php?option=com_cake&module=lodgings&task=feedback&id=1307954819
Can someone help me ? :)
Edit:
I have to write it in .开发者_JAVA技巧htaccess
so I need rewrite rules.
# activate rewrite engine
RewriteEngine On
# mark / as a root
RewriteBase /
# rewrite feedback.php
RewriteCond %{QUERY_STRING} ^rcode=(\d+)
RewriteRule ^feedback.php$ index.php?option=com_cake&module=lodgings&task=feedback&id=%1 [L]
The rule above will rewrite /feedback.php?rcode=1307954819&lang=it
into /index.php?option=com_cake&module=lodgings&task=feedback&id=1307954819
without changing URL in address bar of the browser.
If you need to change URL in address bar as well (to make a redirect) then change [L]
into [R=301,L]
You don't need RegEx for this. You can use str_replace.
$URL='http://myurl/feedback.php?rcode=1307954819&lang=it';
$newURL=str_replace(array('http://myurl/feedback.php','?rcode=','&lang=it'),array('http://myurl/index.php','?option=com_cake&module=lodgings&task=feedback&id=',''),$URL);
Without taking into acount option amd module params:
$url = 'http://*myurl*/feedback.php?rcode=1307954819&lang=it';
echo preg_replace('%^http://([^/]*)/([^.]*)\.php\?rcode=([0-9]*).*$%','http://$1/index.php?option=com_cake&module=lodgings&task=$2&id=$3',$url);
In .htaccess it should be:
RewriteRule ^/([^.]*)\.php\?rcode=([0-9]*).*$ /index.php?option=com_cake&module=lodgings&task=$1&id=$2
My example is in C# (not a php developer), but the regex pattern should work in both languages..
void Main()
{
string input = @"http://myurl/feedback.php?rcode=1307954819&lang=it";
Regex pattern = new Regex(".+?&?rcode=([^&]+)");
string output = pattern.Replace(input, @"http://myurl/index.php?option=com_cake&module=lodgings&task=feedback&id=$1");
Console.WriteLine(output);
}
精彩评论