'How to covert youtube link into embed link in Laravel and validate that field should contain only youtube link
<!DOCTYPE html>
<html>
<body>
<h2>HTML Forms</h2>
<form action="">
<label for="fname">You tube link1:</label><br>
<input type="text" id="youtube_link" name="youtube_link" value="https://www.youtube.com/watch?v=0eKVizvYSUQ"><br>
<label for="lname">you tubelink 2:</label><br>
<input type="text" id="youtube_link2" name="youtube_link2" value="https://www.youtube.com/watch?v=0eKVizvYSUQ"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
here i want to convert the youtube link into embed link in laravel at same time field should contain only youtube links (validation).how to do both in laravel. contoller
public function youtube(Request $request)
{
if ($request->ajax())
{
$rules = [
'youtube_link'=>'?',
'youtube_link2'=>'?',
];
...
}
}
when i click submit i am passing the form data through ajax.
Solution 1:[1]
You can accept only the parameter after v= and keep the field as required.
<!DOCTYPE html>
<html>
<body>
<h2>HTML Forms</h2>
<form action="">
<label for="fname">You tube link1:</label><br>
<input type="text" id="youtube_link" name="youtube_link"
value="0eKVizvYSUQ"><br>
<label for="lname">you tubelink 2:</label><br>
<input type="text" id="youtube_link2" name="youtube_link2"
value="0eKVizvYSUQ"><br><br>
<input type="submit" value="Submit">
</form>
</body>
Now to in validation just put required
public function youtube(Request $request)
{
if ($request->ajax())
{
$rules = [
'youtube_link'=>'required',
'youtube_link2'=>'required',
];
...
}
}
to convert embed links concatenate the input as follows
$data['youtube_link'] = "https://www.youtube.com/embed/" . $request
->youtube_link
$data['youtube_link2'] = "https://www.youtube.com/embed/" . $request
->youtube_link2
To display create an iframe like
<iframe width="853" height="480" src="{{$data->youtube_link}}"
title="YouTube video player" frameborder="0" allow="accelerometer; autoplay;
clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
Hope this will help.
Solution 2:[2]
To verify url parameters you can use the parse_url() method as folows
$link_1 = parse_url("https://www.youtube.com/watch?v=0eKVizvYSUQ");
This will return an array of url parameters like
array:4 [?
"scheme" => "https"
"host" => "www.youtube.com"
"path" => "/watch"
"query" => "v=0eKVizvYSUQ"
]
now you can write your own logic to verify. It may solve your problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bhola Kr. Khawas |
| Solution 2 | Bhola Kr. Khawas |
