Darkman75ful Darkman75ful - 1 month ago 10
PHP Question

Sql request with php : select posts where there is a youtube link

I tried to make a sql request with php which selects posts where there is a youtube link but it doesn't works.

This is what I did :

$bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');
$motif='#http(?:s?)://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)([\w\-\_]+)(&(amp;)?[\w\?=‌​]*)?#';
$sql = "SELECT * FROM posts WHERE post REGEXP :motif";
$params = array(':motif'=>$motif);
$search_for_yt_links = $bdd->prepare($sql);
$search_for_yt_links->execute($params);

$result = $search_for_yt_links->fetch();
var_dump($result);


I get this result :

false


This shuld be :

array (size=1)
'post' => string 'Look at my new video https://www.youtube.com/watch?v=y2Xef0CnUYA. Awesome ?' (length=75)


Unfortunately it doesn't work.
Can you help me ?

Thanks.

Max Max
Answer

This regex detects full links and short ones (youtu.be)

"/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i"
Comments