【问题标题】:preg_match url patternpreg_match 网址格式
【发布时间】:2011-05-24 11:16:49
【问题描述】:

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

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>

请帮帮我。

【问题讨论】:

    标签: php javascript preg-match file-get-contents


    【解决方案1】:

    为什么不使用 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 = "";
    }
    

    【讨论】:

    • 知道了: preg_match('/&lt;a href="javascript:LastURL\(\'(.*?)\'\)/i', $url, $match); 谢谢。
    【解决方案2】:

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

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

    【讨论】:

    • (f|ht){1} == (f|ht){1} 是隐含的。
    • 以及如何在我的情况下使用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2011-06-03
    相关资源
    最近更新 更多