drpqxogph15436713 2011-02-10 15:06
浏览 36
已采纳

php字符串搜索 - 抓取特定网址

I have this string that may contain some urls that I need to grab. For instance, if the user does:

www.youtube ...

or

www.vimeo ...

or

http://www.youtube ...

or

HttP://WwW.viMeo

I need to grab it (until he finds a space perhaps). and store it on a already created array.

The need is to separate the vimeo links from the youtube ones and place each of those on the appropriate video object.

I'm not sure if this is possible, I mean, if the URL coming from the browser could be used to be placed on a predefined video object. If it is, then this is the way to go (so I believe).

If all this is feasible, can I have your help in order to build such a rule?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dtwncxs3547 2011-02-10 15:11
    关注

    This matches the links you need, and store them in a 2D array by site name:

    $video_links = array();
    if (preg_match_all("'(http://)?(www[.])?(youtube|vimeo)[^\s]+'is",$str,$n)) {
        foreach ($n[3] as $key => $site)
        {
            $video_links[$site][] = $n[0][$key];
        }
    }
    

    What does this do?

    This match separates 3 + 1 parts of the needed urls in $str, which is your string:

    • Part 0: the whole match (your video link)
    • Part 1: http:// (optional)
    • Part 2: www. (optional)
    • Part 3: vimeo or youtube

    preg_match_all returns a 2D array with the above part numbers at first level, and every match inside is the part of each match. So you iterate part 3 of the match ($n[3]), and use the array keys to reference part 0 ($n[0][$key]), and arrange them in a nice 2D array like this:

    $video_links = array (
        'vimeo' => array (
            0 => 'vimeo link 1',
            1 => 'vimeo link 2',
            // ...
        ),
        'youtube' => array (
            0 => 'youtube link 1',
            1 => 'youtube link 2',
            // ...
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应