简体   繁体   中英

preg_match url pattern

as I cant find exact answer to my question I decided to ask for help posting my question here. So, I have a page content which I get with file_get_contents and want to preg_match this url:

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

from

<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>

Please help me.

Why not use the DOM? That's what it's for...

If you insist on a regex, try (in PHP)

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

or (in JavaScript)

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

As long as the url is always going to start with http:// then you could use the following expression within your preg_match:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM