0

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?

4

2 回答 2

2

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 />";      
}
于 2012-10-28T10:35:03.813 回答
1
$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 />";      
}
于 2012-10-28T10:32:17.640 回答