如何从URl获取特定文本 [英] How To Get Specific Text From URl

查看:67
本文介绍了如何从URl获取特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Coders ...



http://fayettevillenc.backpage.com/AutosForSale/2008-mercury-milan-i4-used-car-lot-nc/15472524 [ ^ ]





i希望从http到//获取内容。



like在上面的链接我想fayettevillenc ..



你能告诉我怎么做吗?

解决方案

< blockquote>如果你想获得构成完整路径的每个部分,

使用split函数拆分/并使用数组索引获取构成路径的目录的每个部分。 />


现在,如果你想获得查询字符串,

例如www.yourdomain.com/test.aspx?id=1&lang=net& ; category = mvc



哟你可以在javascript中使用一个简单的函数



< br /> 
函数queryString(name){< br />
var match = RegExp('[?&]'+ name +'=([^&] *)')< br />
.exec(window.location.search); < br />
返回匹配&& decodeURIComponent(匹配[1] .replace(/ \ + / g,''));< br />
}< br />





用javascript访问查询字符串

< br /> 
var id = queryString('id');< ; br />
var lang = queryString('lang');< br />







如果你想在客户端的PHP bot中做,你可以使用

PHP拆分功能

这里有一些帮助你的网址,



http://in2.php.net/parse_url [ ^ ]



< a href =http://stackoverflow.com/questions/19989467/php-split-string-on-forward-slash-only-if-it-is-not-preceded-by-a-backslash> http:/ /stackoverflow.com/questions/19989467/php-split-string-on-forward-slash-only-if-it-is-not-preceded-by-a-backslash [ ^ ]



http://www.w3schools.com/php/func_string_str_split.asp [ ^ ]





干杯


Hello Coders ...

http://fayettevillenc.backpage.com/AutosForSale/2008-mercury-milan-i4-used-car-lot-nc/15472524[^]


i want to grab the content from http:// to .

like in the upper link i want fayettevillenc ..

can you plz tell me how to do this ?

解决方案

If you want to get each part that forms a full path,
use split function to split "/" and use the array index to get each part of the directory that forms a path.

Now, if you want to get the querystring,
e.g www.yourdomain.com/test.aspx?id=1&lang=net&category=mvc

You can use a simple function in javascript

<br />
function queryString(name) {<br />
    var match = RegExp('[?&]' + name + '=([^&]*)')<br />
                    .exec(window.location.search);<br />
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));<br />
}<br />



to access the query string in javascript

<br />
var id = queryString('id');<br />
var lang = queryString('lang');<br />




If you want to do it in PHP bot in client side, you can use
PHP split function
Here are some url that will help you,

http://in2.php.net/parse_url[^]

http://stackoverflow.com/questions/19989467/php-split-string-on-forward-slash-only-if-it-is-not-preceded-by-a-backslash[^]

http://www.w3schools.com/php/func_string_str_split.asp[^]


Cheers


这篇关于如何从URl获取特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆