Reputation: 184
i made this code to preg_replace the youtube code, but i got a problem, here is my code
// replace video Youtube to thumbnail
$string = array('http://youtube.com/watch?v=slBEtqC_JsI', 'https://youtu.be/bOxza0rLv1o');
//fix the '/' in the end of url
$string = trim($string, '/');
$search = array();
$search[0] = '~(?:https://(?:www\.)?youtube\.com\/watch\?v=)?([a-zA-Z0-9_\-+?:]+)~';
$search[1] = '~(?:https://(?:www\.)?youtu\.be\/)?([a-zA-Z0-9_\-+?:]+)~';
$replace = array();
$replace[1] = 'https://i.ytimg.com/vi/$1/hqdefault.jpg';
$replace[0] = 'https://i.ytimg.com/vi/$1/hqdefault.jpg';
$soc_youtube = preg_replace($search,$replace,$string);
the code doesn't change the string, the output will be
https://i.ytimg.com/vi/https:/hqdefault.jpg//https://i.ytimg.com/vi/i/hqdefault.jpg.https://i.ytimg.com/vi/ytimg/hqdefault.jpg.https://i.ytimg.com/vi/com/hqdefault.jpg/https://i.ytimg.com/vi/vi/hqdefault.jpg/https://i.ytimg.com/vi/https:/hqdefault.jpg/https://i.ytimg.com/vi/hqdefault/hqdefault.jpg.https://i.ytimg.com/vi/jpg/hqdefault.jpg//https://i.ytimg.com/vi/https:/hqdefault.jpg//https://i.ytimg.com/vi/i/hqdefault.jpg.https://i.ytimg.com/vi/ytimg/hqdefault.jpg.https://i.ytimg.com/vi/com/hqdefault.jpg/https://i.ytimg.com/vi/vi/hqdefault.jpg/https://i.ytimg.com/vi/youtu/hqdefault.jpg/https://i.ytimg.com/vi/hqdefault/hqdefault.jpg.https://i.ytimg.com/vi/jpg/hqdefault.jpg.https://i.ytimg.com/vi/https:/hqdefault.jpg//https://i.ytimg.com/vi/i/hqdefault.jpg.https://i.ytimg.com/vi/ytimg/hqdefault.jpg.https://i.ytimg.com/vi/com/hqdefault.jpg/https://i.ytimg.com/vi/vi/hqdefault.jpg/https://i.ytimg.com/vi/be/hqdefault.jpg/https://i.ytimg.com/vi/hqdefault/hqdefault.jpg.https://i.ytimg.com/vi/jpg/hqdefault.jpg/https://i.ytimg.com/vi/https:/hqdefault.jpg//https://i.ytimg.com/vi/i/hqdefault.jpg.https://i.ytimg.com/vi/ytimg/hqdefault.jpg.https://i.ytimg.com/vi/com/hqdefault.jpg/https://i.ytimg.com/vi/vi/hqdefault.jpg/https://i.ytimg.com/vi/bOxza0rLv1o/hqdefault.jpg/https://i.ytimg.com/vi/hqdefault/hqdefault.jpg.https://i.ytimg.com/vi/jpg/hqdefault.jpg);
anyone can help me with this? thanks for your help
Upvotes: 1
Views: 59
Reputation: 626932
You may use
'~(?:https?://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/))?([a-zA-Z0-9_\-+?:]+)/?~'
See the regex demo. Pattern details:
(?:
- start of the 1st alternation group:
https?://(?:www\.)?youtu
- https://
or https://
, then an optional www.
substring and then youtu
substring(?:
- start of the 2nd alternation group:
be\.com/watch\?v=
- be.com/watch?v=
substring|
- or\.be/
- .be/
substring)
- end of the 2nd alternation group)?
- 1 or 0 times([a-zA-Z0-9_\-+?:]+)
- Group 1: one or more word, -
, +
, ?
or :
chars/?
- an optional /
See the PHP demo online:
$string = array('http://youtube.com/watch?v=slBEtqC_JsI', 'https://youtu.be/bOxza0rLv1o', 'http://youtube.com/watch?v=slBEtqC_JSJ/');
$search = '~(?:https?://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/))?([a-zA-Z0-9_+?:-]+)/?~';
$replace = 'https://i.ytimg.com/vi/$1/hqdefault.jpg';
$soc_youtube = preg_replace($search,$replace,$string);
print_r($soc_youtube);
Output:
Array
(
[0] => https://i.ytimg.com/vi/slBEtqC_JsI/hqdefault.jpg
[1] => https://i.ytimg.com/vi/bOxza0rLv1o/hqdefault.jpg
[2] => https://i.ytimg.com/vi/slBEtqC_JSJ/hqdefault.jpg
)
Upvotes: 1