Jump to content

youtube video id regex issue


phpsycho

Recommended Posts

I got this code..

$expr = '\(?:http://){0,1}(?:www.){0,1}youtube.com/watch\?.*?v=([^&[]++)[^[]*+/is';
$replacement = '${1}1,$3';
$string = preg_replace($expr, $replacement, $string);

I am not familiar with regex well enough to understand the problem here. the \ in the front of $expr and the /is at the end is causing this code not to work. No idea what I am supposed to have there instead. Been messing around with the code but can't figure out how to fix it.

 

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /var/www/se1/includes/functions.php on line 74 Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /var/www/se1/includes/functions.php on line 74 Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /var/www/se1/includes/functions.php on line 74 Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /var/www/se1/includes/functions.php on line 74 Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /var/www/se1/includes/functions.php on line 74

Link to comment
https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/
Share on other sites

hmmm another problem..

I needed this to remove anything after the video id but once there is white space to stop. because right now basically I have the link and then I have text, the text is being removed and in another case the text is being inserted along with the video id.

<?php
$string = "http://www.youtube.com/watch?v=4JipHEz53sU <br><br>testing youtube videos ^^";
$expr = '~(?:http://){0,1}(?:www.){0,1}youtube.com/watch\?.*?v=([^&[]++)[^[]*+~is';
$replacement = '<iframe width="475" height="300" src="http://www.youtube.com/embed/${1}" frameborder="0" allowfullscreen></iframe>'; //${1} = 4JipHEz53sU <br><br>testing youtube videos ^^ 
$string = preg_replace($expr, $replacement, $string);
?>

You're going to shoot me lol I'm sorry, didn't explain it very well.

The text that is still being left is: &feature=related

so I want anything that comes after the video id v= to be removed. because there are different things that tend to come after the video id.

the text after &feature=related should be left alone. Just this code here that was supposed to remove the &feature=related, [^[]*]+ would remove everything including the text after the link which shouldn't be removed.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.