免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2764 | 回复: 5

如何获取URL字符串中的某个变量的值 ? [复制链接]

论坛徽章:
0
发表于 2006-11-06 19:37 |显示全部楼层
如题。

$url = 'http://localhost/a.asp?id=1&cid=4&tid=5';

要从字符串中获取id的值用正则表达式怎么取?
其中ID位置不固定 ,也有可能
$url = 'http://localhost/a.asp?cid=4&tid=5&id=1';

论坛徽章:
4
处女座
日期:2013-09-01 19:30:07双鱼座
日期:2013-09-28 10:58:45双子座
日期:2014-05-23 08:32:0215-16赛季CBA联赛之八一
日期:2016-04-12 19:55:42
发表于 2006-11-06 19:50 |显示全部楼层
xx.pl

$url = 'http://localhost/a.asp?id=1&cid=4&tid=5';
$url=~m/(\&|\?)id\=(\d+)/i;
print "$2\n";

$url = 'http://localhost/a.asp?cid=4&tid=5&id=10';
$url=~m/(\&|\?)id\=(\d+)/i;
print $2;

D:\>xx.pl
1
10

论坛徽章:
0
发表于 2006-11-06 22:17 |显示全部楼层

  1. #!/usr/bin/perl
  2. $url1 = 'http://localhost/a.asp?id=1&cid=4&tid=5';
  3. $url2 = 'http://localhost/a.asp?cid=4&tid=5&id=1';
  4. print $1,"\n" if($url1=~/(?<=\?|&)id=(\d+)/);
  5. print $& if($url2=~/(?<=\?|&)id=\d+/);
复制代码

output

  1. 1
  2. id=1
复制代码

[ 本帖最后由 alexru 于 2006-11-6 22:33 编辑 ]

论坛徽章:
0
发表于 2006-11-06 23:36 |显示全部楼层
verygood,我还以为要用到hash.用正则最简单

论坛徽章:
0
发表于 2006-11-06 23:50 |显示全部楼层
原帖由 yhsmengdi 于 2006-11-6 19:37 发表
如题。

$url = 'http://localhost/a.asp?id=1&cid=4&tid=5';

要从字符串中获取id的值用正则表达式怎么取?
其中ID位置不固定 ,也有可能
$url = 'http://localhost/a.asp?cid=4&tid=5&id ...


Hi,

generally regex is used.
But you can use CPAN module URI to fetch everything about uri.
For example in your case:
use URI;
use strict;

my $u=URI->new($url);
my %param = $u->query_form();
print $param{"id"};


see more => perldoc URI

regards, ulmer

论坛徽章:
4
处女座
日期:2013-09-01 19:30:07双鱼座
日期:2013-09-28 10:58:45双子座
日期:2014-05-23 08:32:0215-16赛季CBA联赛之八一
日期:2016-04-12 19:55:42
发表于 2006-11-07 09:41 |显示全部楼层
原帖由 ulmer 于 2006-11-6 23:50 发表


Hi,

generally regex is used.
But you can use CPAN module URI to fetch everything about uri.
For example in your case:


see more => perldoc URI

regards, ulmer


that's fine
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP