Php JavaScript:重写url时获取原始查询字符串
我的页面的url被重写如下 www.test-me.com/book-cat-white.HTMLPhp JavaScript:重写url时获取原始查询字符串,php,javascript,query-string,Php,Javascript,Query String,我的页面的url被重写如下 www.test-me.com/book-cat-white.HTML book是book.php 猫是动物 白色就是颜色=白色 原始URL是 www.test-me.com/book.php?动物=猫&颜色=白色 使用PHP,我可以使用$\u-get获取值,例如$\u-get['animal']。如何在JavaScript中实现同样的功能?我是否可以访问一个变量来返回查询字符串?您只能访问“public”变量,因为浏览器会看到它 如果您需要内部的、已处理的、未重
- book是book.php
- 猫是动物
- 白色就是颜色=白色
$\u-get
获取值,例如$\u-get['animal']
。如何在JavaScript中实现同样的功能?我是否可以访问一个变量来返回查询字符串?您只能访问“public”变量,因为浏览器会看到它
如果您需要内部的、已处理的、未重写的参数,我建议您将它们从PHP中写入标题
:
<script type="text/javascript">
query_animal = "<?php echo htmlspecialchars($_GET["animal"]); ?>";
query_color = "<?php echo htmlspecialchars($_GET["color"]); ?>";
</script>
查询_animal=“”;
查询_color=“”;
您可以使用及其属性,但这只指向http://WWW.test-me.com/book-cat-white.HTML
--服务器端重写的GET参数将不可用
你可以试试:
var match = window.location.pathname.match(/\/book-([^-]+)-([^-]+).html$/i);
// for your example - match contains: ["/book-cat-white.HTML", "cat", "white"]
对正则表达式的进一步解释:
/ # Start Expression
\/book- # Match '/book-'
( # Start Capture Group 1
[^-]+ # Match any character other than '-' 1 or more times
) # End Capture Group 1
- # Match '-'
( # Start Capture Group 2
[^-]+ # Match any character other than '-' 1 or more times
) # End Capture Group 2
.html # Match '.html'
$ # Match the end of the string
/i # End Expression - Case insensitive flag
PHP5.2+您只需执行
var$\u GET=代码>但是这个cat-white.html不是静态的,这是动态的…有时URL可能是。。。。狗褐色。html@Bharanikumar-是,如果URL与RegExp匹配。。。即是/book dog brown.html
则匹配[1]
将是狗
,匹配[2]
将是棕色