如何在php中解析你的管代码

时间:2014-10-09 07:05:26

标签: php regex

我需要什么

  • 我需要11位数的代码,这些代码应该附加在你的管道网址中。

     like www.youtube.com/embed/XzqagJTsNrc.
    

源代码

 $url= htmlentities($this->linkify($row['tweetText']));

 $youtube=explode(' ',$url).

数组输出。

 Array
(
[0] => VW
[1] => @
[2] => European
[3] => Motor
[4] => Show
[5] => Brussels
[6] => -
[7] => Behind
[8] => the
[9] => scene
[10] => (part
[11] => 1):
[12] =>  <a
[13] => class="twtlnk"
[14] => rel="nofollow"
[15] => href="http://youtu.be/XzqagJTsNrc"
[16] => target=\"_blank\">http://youtu.be/XzqagJTsNrc</a> 
[17] => via
[18] =>  <a
[19] => class="twthand"
[20] => href="https://twitter.com/youtube"
[21] => rel="nofollow"
[22] => target=\"_blank\"
[23] => >@youtube</a>
)

我尝试过使用正则表达式

           $text = preg_replace('~

            https?://         # Required scheme. Either http or https.
            (?:[0-9A-Z-]+\.)? # Optional subdomain.
            (?:               # Group host alternatives.
            youtu\.be/      # Either youtu.be,
            | youtube         # or youtube.com or
            (?:-nocookie)?  # youtube-nocookie.com
            \.com           # followed by
            \S*             # Allow anything up to VIDEO_ID,
            [^\w\s-]       # but char before ID is non-ID char.
            )                 # End host alternatives.
            ([\w-]{11})      # $1: VIDEO_ID is exactly 11 chars.
            (?=[^\w-]|$)     # Assert next char is non-ID or EOS.
            (?!               # Assert URL is not pre-linked.
            [?=&+%\w.-]*    # Allow URL (query) remainder.
            (?:             # Group pre-linked alternatives.
            [\'"][^<>]*>  # Either inside a start tag,
            | </a>          # or inside <a> element text contents.
            )               # End recognized pre-linked alts.
            )                 # End negative lookahead assertion.
            [?=&+%\w.-]*        # Consume any URL (query) remainder.
            ~ix', 
            '<a href="http://www.youtube.com/watch?v=$1">$1</a>',
            $youtube);

            print_r($text);

数组返回

  Array
 (
[0] => VW
[1] => @
[2] => European
[3] => Motor
[4] => Show
[5] => Brussels
[6] => -
[7] => Behind
[8] => the
[9] => scene
[10] => (part
[11] => 1):
[12] => &amp;nbsp;&lt;a
[13] => class=&quot;twtlnk&quot;
[14] => rel=&quot;nofollow&quot;
[15] => href=&quot;<a href="http://www.youtube.com/watch?v=XzqagJTsNrc">XzqagJTsNrc</a>;
[16] => target=\&quot;_blank\&quot;&gt;<a href="http://www.youtube.com/watch?v=XzqagJTsNrc">XzqagJTsNrc</a>;/a&gt;&nbsp;
[17] => via
[18] => &amp;nbsp;&lt;a
[19] => class=&quot;twthand&quot;
[20] => href=&quot;https://twitter.com/youtube&quot;
[21] => rel=&quot;nofollow&quot;
[22] => target=\&quot;_blank\&quot;
[23] => &gt;@youtube&lt;/a&gt;

  • 我只需要获取XzqagJTsNrc;在做print_r($ text [15]);
  • 输出href =&#34; XzqagJTsNrc;
  • 但我只需要XzqagJTsNrc(11位数字)。

        how to parse   href="XzqagJTsNrc; (href =" ;)
    

3 个答案:

答案 0 :(得分:1)

您可以使用preg_match

执行此操作
$url = $text[15];

if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
   $video_id = $match[1];
}

echo( $video_id);

它不仅会匹配youtube,还会匹配其他ID

答案 1 :(得分:0)

简单:

$var = 'href="XzqagJTsNrc;';
$youtube_id = substr($var, 6, 11);

答案 2 :(得分:0)

如果您只需要XzqagJTsNrc,请使用basename

$link = 'http://www.youtube.com/embed/XzqagJTsNrc';
echo basename($link); // XzqagJTsNrc