preg_match url pattern


preg_match url pattern

由于我找不到问题的确切答案,我决定在这里寻求帮助发布我的问题。所以,我有一个页面内容,我用file_get_contents得到,并想preg_match这个url:

http://sample.com/Last/LastSearch.ashx?q=cnt=1&day=5&keyword=sample&location=15&view=v

来自

<a href="javascript:LastURL('http://sample.com/Last/LastSearch.ashx?q=cnt=1&day=5&keyword=sample&location=15&view=v');" id="Last" rel="nofollow" class="Last" onclick="javascript:hideCss('LastCSS');hideCss('FirstRCSS');">Last</a>

请帮帮我。

为什么不使用DOM?这就是它的用途。。。

如果您坚持使用正则表达式,请尝试(在PHP中)

if (preg_match('/<a href="javascript:LastURL'(''([^''])*''/', $subject, $regs)) {
    $result = $regs[1];
} else {
    $result = "";
}

或(在JavaScript中)

var myregexp = /<a href="javascript:LastURL'('([^'])*'/;
var match = myregexp.exec(subject);
if (match != null) {
    result = match[1];
} else {
    result = "";
}

只要url总是以http://开头,那么您就可以在preg_match中使用以下表达式:

(((f|ht){1}tp://)[-a-zA-Z0-9@:%_'+.~#?&//=]+)