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
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.

Link to comment
Share on other sites

<?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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.