phpsycho Posted July 10, 2011 Share Posted July 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/ Share on other sites More sharing options...
.josh Posted July 10, 2011 Share Posted July 10, 2011 $expr = '~(?:http://){0,1}(?:www.){0,1}youtube.com/watch\?.*?v=([^&[]++)[^[]*+~is'; Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1240975 Share on other sites More sharing options...
phpsycho Posted July 10, 2011 Author Share Posted July 10, 2011 I feel so dumb lol such a simple fix. thank you though, very much! Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1240980 Share on other sites More sharing options...
phpsycho Posted July 10, 2011 Author Share Posted July 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1240989 Share on other sites More sharing options...
.josh Posted July 10, 2011 Share Posted July 10, 2011 How about you show example of actual content you are trying to make changes to (what is actually in $string), including a "before" and "after". Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1241000 Share on other sites More sharing options...
phpsycho Posted July 10, 2011 Author Share Posted July 10, 2011 <?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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1241013 Share on other sites More sharing options...
.josh Posted July 11, 2011 Share Posted July 11, 2011 $expr = '~youtube\.com/watch(??:(?!v=|\s).)*)v=([-a-z0-9_]+)~i'; Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1241018 Share on other sites More sharing options...
phpsycho Posted July 11, 2011 Author Share Posted July 11, 2011 That works but its leaving the text there.. can it be totally removed? Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1241022 Share on other sites More sharing options...
.josh Posted July 11, 2011 Share Posted July 11, 2011 ah okay $expr = '~(?:http://)?(?:www\.)?youtube\.com/watch(??:(?!v=|\s).)*)v=([-a-z0-9_]+)~i'; Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1241027 Share on other sites More sharing options...
.josh Posted July 11, 2011 Share Posted July 11, 2011 that will also remove the http:// and www. but that stuff after the link... " <br><br>testing youtube videos ^^"" you're gonna have to explain what all that is about...no real easy way to get rid of arbitrary text like that... Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1241028 Share on other sites More sharing options...
phpsycho Posted July 11, 2011 Author Share Posted July 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1241031 Share on other sites More sharing options...
.josh Posted July 11, 2011 Share Posted July 11, 2011 $expr = '~(?:http://)?(?:www\.)?youtube\.com/watch(??:(?!v=|\s).)*)v=([-a-z0-9_]+)[^ ]*~i'; Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1241033 Share on other sites More sharing options...
phpsycho Posted July 11, 2011 Author Share Posted July 11, 2011 That works perfectly! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/241607-youtube-video-id-regex-issue/#findComment-1241038 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.