search
Homephp教程php手册php 从url中提取参数名和参数值实例

如果我使用的是动态url后面会带上如a.php?a=1&b=2这种,那么我要提取a,b参数名与参数值要怎么操作呢,下面我来介绍一个自己用的实例.

在已知参数名的情况下,获取参数值,使用正则表达式能很容易做到,js的实现方法如下,代码如下:

<?php
function getValue(url, name) {
    var reg = new RegExp(&#39;(?|&)&#39; + name + &#39;=([^&?]*)&#39;, &#39;i&#39;);
    var arr = url . match(reg);
    if (arr) {
        return arr[2];
    }
    return null;
}
?>

如果想获取所有的参数名和其对应的值,同样也可以使用正则表达式的方法,js实现方法,代码如下:

<?php
function getKeyValue(url) { 
 var result = {}; 
 var reg = new RegExp(&#39;([?|&])(.+?)=([^&?]*)&#39;, &#39;ig&#39;); 
 var arr = reg.exec(url); 
 while (arr) { 
  result[arr[2]] = arr[3]; 
  arr = reg.exec(url); 
 } 
 return result; 
}
?>

注意在js中用于匹配的还有一个叫match,match是字符串的方法,而exec是RegExp对象的方法,使用字符串的match方法,同时正则表达式指定为全局匹配,那么正则表达式中的分组将没有用,返回的结果是所有匹配正则表达式的子字符串,exec方法没有使用全局匹配标志时,返回的时间第一个匹配的子字符,如果使用了全局匹配标志,第一次执行从头开始匹配符号的字符串,再次调用,就从上一次匹配结果后开始匹配.

下面提供php的实现方法,代码如下:

<?php
function getKeyValue($url) {
    $result = array();
    $mr = preg_match_all(&#39;/(?|&)(.+?)=([^&?]*)/i&#39;, $url, $matchs);
    if ($mr !== FALSE) {
        for ($i = 0; $i < $mr; $i++) {
            $result[$matchs[2][$i]] = $matchs[3][$i];
        }
    }
    return $result;
}
?>

php的preg_match_all方法把匹配的结果存放在第三个指定的参数中,是一个二维数组,第一维度是分组信息的数组,即第一个数组存放的是所有匹配的完整字符串,第二个数组存放的是第一个()对应的值得,第二维度是分组的值.


永久链接:

转载随意!带上文章地址吧。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software