/    Sign up×
Community /Pin to ProfileBookmark

youtube links to embed

allowing users to enter messagein textarea.
user can post youtube video links anywhere in message.

wanted to modify this message in serverside php.
Convert all youtube links to youtube embed code.
Got below code from a site.
But it ignores all other than youtube links.

[CODE]<?php
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’){

$input=$_POST[‘html’];
preg_match_all(‘#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch?v=([-|~_0-9A-Za-z]+)&?.*?)#i’,$input,$output);
foreach ($output[4] AS $video_id) {
if (!isset($video[$video_id])) {
$video[$video_id]=true;
$embed_code = ‘<object width=”300″ height=”300″><param name=”movie” value=”http://www.youtube.com/v/’.$video_id.’&hl=en&fs=1&”></param><param name=”allowFullScreen” value=”true”></param><param name=”allowscriptaccess” value=”always”></param><embed src=”http://www.youtube.com/v/’.$video_id.’&hl=en&fs=1&” type=”application/x-shockwave-flash” allowscriptaccess=”always” allowfullscreen=”true” width=”300″ height=”300″></embed></object>’;
echo $embed_code.'<br>’;
}
}
}
?>[/CODE]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@arrielmabaleAug 15.2010 — allowing users to enter messagein textarea.

user can post youtube video links anywhere in message.

wanted to modify this message in serverside php.

Convert all youtube links to youtube embed code.

Got below code from a site.

But it ignores all other than youtube links.

[CODE]<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){

$input=$_POST['html'];
preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch?v=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output);
foreach ($output[4] AS $video_id) {
if (!isset($video[$video_id])) {
$video[$video_id]=true;
$embed_code = '<object width="300" height="300"><param name="movie" value="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="300" height="300"></embed></object>';
echo $embed_code.'<br>';
}
}
}
?>[/CODE]
[/QUOTE]


this code
[code=php]preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch?v=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output);[/code]

filters only youtube links, rephrase the expression and allow other site's to be processed ?
×

Success!

Help @iusephp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...