Preg_match -从字符串中提取id

时间:2022-09-13 07:34:52
<content type='application/x-shockwave-flash' src='https://www.youtube.com/v/f4LxBKN9ank?version=3&amp;f=videos&amp;app=youtube_gdata'/>

How could I get only the f4LxBKN9ank number? all my attepts to construct the preg_match failed. Thanks for tips.

我怎么能只得到f4LxBKN9ank的号码?我构造preg_match的所有证明都失败了。谢谢你的建议。

EDIT

编辑

<yt:statistics favoriteCount='0' viewCount='106281'/>

<yt:rating numDislikes='9' numLikes='569'/>

How to retrive viewCount and the numDislikes/Likes

如何检索viewCount和数字/喜欢项

2 个解决方案

#1


2  

Not that cool but regular expression are not that "fast". str_replace is much faster. BTW: "parse_url" rocks: http://www.php.net/manual/de/function.parse-url.php

不是很酷,但正则表达式并不那么“快”。是大小写不敏感更快。顺便说一句:“parse_url”岩石:http://www.php.net/manual/de/function.parse-url.php

<?php 

$data = str_replace(array("path" => "/v/"),array(''),parse_url('https://www.youtube.com/v/f4LxBKN9ankversion=3&amp;f=videos&amp;app=youtbe_gdata'));

var_dump($data['path']);
// string 'f4LxBKN9ank' (length=11)

#2


1  

You can try with:

你可以尝试:

<?php
preg_match('#www\.youtube\.com\/v\/([\w\-]+)\?#', $string, $match);

$id = $match[1];
echo $id;

For your other question, I strongly discourage you to use a regex for parsing HTML/XML.
You should go for something like phpQuery or DOM.

对于您的另一个问题,我强烈建议您不要使用regex解析HTML/XML。您应该使用phpQuery或DOM。

#1


2  

Not that cool but regular expression are not that "fast". str_replace is much faster. BTW: "parse_url" rocks: http://www.php.net/manual/de/function.parse-url.php

不是很酷,但正则表达式并不那么“快”。是大小写不敏感更快。顺便说一句:“parse_url”岩石:http://www.php.net/manual/de/function.parse-url.php

<?php 

$data = str_replace(array("path" => "/v/"),array(''),parse_url('https://www.youtube.com/v/f4LxBKN9ankversion=3&amp;f=videos&amp;app=youtbe_gdata'));

var_dump($data['path']);
// string 'f4LxBKN9ank' (length=11)

#2


1  

You can try with:

你可以尝试:

<?php
preg_match('#www\.youtube\.com\/v\/([\w\-]+)\?#', $string, $match);

$id = $match[1];
echo $id;

For your other question, I strongly discourage you to use a regex for parsing HTML/XML.
You should go for something like phpQuery or DOM.

对于您的另一个问题,我强烈建议您不要使用regex解析HTML/XML。您应该使用phpQuery或DOM。