4

我试图找出一种从文本字符串中获取 URL 数组的方法。文本将有点像这样的格式:

一些随机文字在这里

http://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-iphones-bezel-a-massive-notification-light/?grcc=88888Z0ZwdgtZ0Z0Z0Z0Z0&grcc2=835637c33f965e6cdd34c87219233711~1342828462249~fca4fa8af1286d8a77f26033fdeed202~510f37324b14c50a5e9121f955fac3fa ~1342747216490~0~0~0~0~0~0~0~0~7~3~

http://techcrunch.com/2012/07/20/last-day-to-purchase-extra-early-bird-tickets-for-disrupt-sf/

显然,这些链接可以是任何东西(并且可以有很多链接,这些只是我现在正在测试的链接。如果我使用像我的正则表达式这样的简单 URL 就可以了。

我在用:

preg_match_all('((https?|ftp|gopher|telnet|file|notes|ms-help):'.
    '((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)',
    $bodyMessage, $matches, PREG_PATTERN_ORDER);

当我这样做时print_r( $matches);,我得到的结果是:

Array ( [0] => Array (
    [0] => http://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-iphon=
    [1] => http://techcrunch.com/2012/07/20/last-day-to-purchase-extra-early-bird-tick= 
    [2] => http://techcrunch.co=
    [3] => http://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-ip= 
    [4] => http://techcrunch.com/2012/07/20/last-day-to-purc=
    [5] => http://tec=
)
...

该数组中的这些项目都不是来自上述链接的完整链接。

有人知道获得我需要的东西的好方法吗?我发现了一堆正则表达式的东西来获取 PHP 的链接,但没有一个有效。

谢谢!

编辑:

好的,所以我从电子邮件中提取这些链接。该脚本解析电子邮件,获取邮件正文,然后尝试从中获取链接。调查电子邮件后,似乎出于某种原因在 url 中间添加了一个空格。这是我的 PHP 脚本看到的正文消息的输出。

 --00248c711bb99ca36d04c54ba5c6 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable http://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-iphon= es-bezel-a-massive-notification-light/?grcc=3D88888Z0ZwdgtZ0Z0Z0Z0Z0&grcc2= =3D835637c33f965e6cdd34c87219233711~1342828462249~fca4fa8af1286d8a77f26033f= deed202~510f37324b14c50a5e9121f955fac3fa~1342747216490~0~0~0~0~0~0~0~0~7~3~ http://techcrunch.com/2012/07/20/last-day-to-purchase-extra-early-bird-tick= ets-for-disrupt-sf/ --00248c711bb99ca36d04c54ba5c6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 

有关如何使其不破坏 URLS 的任何建议?

编辑 2

根据劳内特的建议,我运行了这段代码:

 $bodyMessage = str_replace("= ", "",$bodyMessage);

但是,当我回显时,它似乎不想替换“=”

 --00248c711bb99ca36d04c54ba5c6 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable http://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-iphon= es-bezel-a-massive-notification-light/?grcc=3D88888Z0ZwdgtZ0Z0Z0Z0Z0&grcc2= =3D835637c33f965e6cdd34c87219233711~1342828462249~fca4fa8af1286d8a77f26033f= deed202~510f37324b14c50a5e9121f955fac3fa~1342747216490~0~0~0~0~0~0~0~0~7~3~ http://techcrunch.com/2012/07/20/last-day-to-purchase-extra-early-bird-tick= ets-for-disrupt-sf/ --00248c711bb99ca36d04c54ba5c6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 
4

4 回答 4

9
    /**
     *
     * @get URLs from string (string maybe a url)
     *
     * @param string $string

     * @return array
     *
     */
    function getUrls($string) {
        $regex = '/https?\:\/\/[^\" ]+/i';
        preg_match_all($regex, $string, $matches);
        //return (array_reverse($matches[0]));
        return ($matches[0]);
}
于 2012-07-21T00:48:55.603 回答
0

请改用以下正则表达式。

$regex = "(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))";

希望能帮助到你。

于 2013-07-04T15:42:22.603 回答
0

使用以下代码,您将找到一个数组 urls_in_string,并且在零索引 $urls_in_string[0] 处,您将找到所有 url。

    $urls_in_string = [];
    $string_with_urls = "Worlds most popular socila networking website in https://www.facebook.com. We have many such othe websites like https://twitter.com/home and https://www.linkedin.com/feed/ etc.";
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\/\S*)?/im";
    preg_match_all($reg_exUrl, $string_with_urls, $urls_in_string);
    print_r($urls_in_string);




// OutPut 
/*
Array
(
    [0] => Array
        (
            [0] => https://www.facebook.com
            [1] => https://twitter.com/home
            [2] => https://www.linkedin.com/feed/
        )

    [1] => Array
        (
            [0] => https
            [1] => https
            [2] => https
        )

    [2] => Array
        (
            [0] => 
            [1] => /home
            [2] => /feed/
        )

)
*/
于 2021-08-27T07:49:02.123 回答
0
you can do something like following

$url = "http://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-iphones-bezel-a-massive-notification-light/?grcc=88888Z0ZwdgtZ0Z0Z0Z0Z0&grcc2=835637c33f965e6cdd34c87219233711~1342828462249~fca4fa8af1286d8a77f26033fdeed202~510f37324b14c50a5e9121f955fac3fa~1342747216490~0~0~0~0~0~0~0~0~7~3~

http://techcrunch.com/2012/07/20/last-day-to-purchase-extra-early-bird-tickets-for-disrupt-sf/";

$dataArray = explode("http",$url);

echo "<pre>";print_r($dataArray);

this will return like following array

Array
(
 [0] => 
 [1] => ://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-iphones-bezel-a-massive-notification-light/?grcc=88888Z0ZwdgtZ0Z0Z0Z0Z0&grcc2=835637c33f965e6cdd34c87219233711~1342828462249~fca4fa8af1286d8a77f26033fdeed202~510f37324b14c50a5e9121f955fac3fa~1342747216490~0~0~0~0~0~0~0~0~7~3~


 [2] => ://techcrunch.com/2012/07/20/last-day-to-purchase-extra-early-bird-tickets-for-disrupt-sf/
)

when you extract above output please prepend http, I think this will help you 

Happy Coding
于 2021-08-27T08:59:56.633 回答