在PHP中重现jquery match()
问题描述:
I have written the code in javascript but i need it to happen in serverside, The function gets the part of the requested url and if it's match my switch it's change the ducoment title.
Here is the Javascript:
$(document).ready(function(e){
e=window.location.href;
if(e.match(/\\?case=(.*)/)){
x=e.match(/\\?case=(.*)/);
y=x[1];
switch(y){
case :"t"
document.title ="Title2";
break;
case :"s"
document.title ="Title3";
break;
default:
/* code ... */
break;
}
}
});
In this code if my url is "http://url.com/?case=t" the title of document going to change to "Title2".
How can i write it in php? the problem is getting the url match!
我已经用javascript写了代码,但我需要在服务器端进行,函数获取所请求的部分 url如果它与我的开关匹配,它会更改ducoment标题。 p>
这是Javascript: p>
$(document).ready( function(e){
e = window.location.href;
if(e.match(/ \\?case =(。*)/)){
x = e.match(/ \\?case =( 。*)/);
y = x [1];
开关(y){
case:“t”
document.title =“Title2”;
break;
case:“s”\ n document.title =“Title3”;
break;
默认值:
/ * code ... * /
break;
}
}
});
code> pre>
在此代码中,如果我的网址是“ http://url.com/? case = t “将要更改为”Title2“的文档标题。 p>
如何在php中编写它? 问题是获取网址匹配! p>
div>
答
You mean getting the variable case? Because that's what you used in JavaScript
And if so, an example in PHP would be having this in the header
<?php
$case = $_GET['case'];
switch ($case) {
case 't':
$title = 'Title 2';
break;
}
echo "<title>". $title . "</title";
?>