从数据库填充数组,在里面搜索并提取字符串[关闭]

从数据库填充数组,在里面搜索并提取字符串[关闭]

问题描述:

I am grabbing from a MySQL database some fields and putting them into an array. Here is the code:

$dataArray = array();

$query_url = "SELECT * FROM videos";
$query_exec = mysql_query($query_url) or die();

while($res = mysql_fetch_array($query_exec)) {
    echo $dataArray[$res['id']] = $res['video'];
    echo "<br />";      
}

With this I have the printed list of youtube videos taken from my "videos" table, I am trying to get only the last shortcode of youtube, for example: Here is the youtube link: http://www.youtube.com/watch?v=sN8grtFUQYs I want to get only the: sN8grtFUQYs

How can I get them from an array that gets the data from a MySQL database automatically?

Probably not really efficient but you could explode() it and call end() to get the last result of the array.

$dataArray = array();

$query_url = "SELECT * FROM videos";
$query_exec = mysql_query($query_url) or die();

while($res = mysql_fetch_array($query_exec)) {
    echo $dataArray[$res['id']] = end(explode('?v=',$res['video']));
    echo "<br />";      
}

$dataArray = array();

$query_url = "SELECT * FROM videos";
$query_exec = mysql_query($query_url) or die();

while($res = mysql_fetch_array($query_exec)) {
    preg_match( '/[\?\&]v=([^\?\&]+)/', $res['video'], $id );
    echo $dataArray[$res['id']] = $id[1];
    echo "<br />";      
}