Parsing comments, finding links and embedded video - php

Right now I have a variable: $blogbody which contains the entire contents of a blog.
I'm using the following to convert URLS to clickable links:
$blogbody = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","\\0", $blogbody);
And the following to resize embedded video:
$blogbody = preg_replace('/(width)=("[^"]*")/i', 'width="495"', $blogbody);
The problem I'm running into is the embedded video not working, comes back with an Access Forbidden error (403). If I remove the line to convert URLS to links, the embedded video works fine. Not sure how to get these two working together. If anyone else has a better solution to converting URLS to clickable links and resizing embedded video let me know!

This might be happening because the link which you use to embed the video also gets his <a href=''> tags added. So instead of just converting all links, check that they don't have ' or " directly behind or in front of them - this will make sure that the embedded videos' links won't get anchor tags.

Related

Laravel / PHP - Convert plain links in text to nice links with page title

I've searched a lot but couldn't really find any relevant discussion. Here's the case at my hand -
When a user writes a blog post, they'll simply add plain links in the text. Viz. https://somedomain.com . Now my editor can convert these links to actual html links viz. <a href="https://somedomain.com>https://somedomain.com</a>.
Now I want to extract all such links in the body and convert them into links with page titles. That is, the link should get converted to <a href="https://somedomain.com>Page Title</a>
I've been able to extract the URLs, but the problem is that I can't find a way to fetch the page title.
$dom->loadHTML($request->body);
$urls = $dom->getElementsByTagName('a');
foreach($urls as $url) {
$href = $url->getAttribute('href');
// How do we get the title of this href, now?
}
PS: Would really appreciate if you could suggest a way to do this - or maybe refer a package that does this. I've found a package called 'ESSENCE' but that only works for media files.

Playing music on a website with PHP

So for my Garry's Mod server, I have a loading screen using my website. I only have an image currently, but I wan't to make it play a song while the user waits while loading. My plan is that I make a list of YouTube links, and the PHP coding picks a random link, and plays that song. I have no clue on how to use PHP except for variables.
As per your question, I suggest to you one example. So, Try the code,
Youtube URL look like this : http://www.youtube.com/watch?v=yWsqTrDJ_gU
The part you're going to want to copy is everything after the equals sign, or in the example above: yWsqTrDJ_gU
Copy on that text into a text file, hit Enter and paste everything after the equals sign of the URL on the next video page on the following line. You'll end up with a series of lines that look something like this:
5MQ0QX870FE
yWsqTrDJ_gU
JyTawuNvQi0
HwRQ9dbti-4
_ziv_WeBLvo
Save this text file with a meaningful name and upload it to your Web server. Next you need to build the code to display your video. If you copy the following and paste it onto a page that supports PHP, replacing only the YourVideoList.txt with the path to your file, you should get a YouTube player with a randomized set of videos.
<?php
// Build an array from the list of YouTube videos
// Replace YourVideoList.txt with the path to your text file
// This will likely be something like /home/accountname/public_html/foldername/etc
$video_array = file('YourVideoList.txt');
// Randomly pick one video from the array
$video = $video_array[rand(0, count($video_array) - 1)];
$video = trim($video);
?>
<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/<?php echo $video;? >"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/<?php echo $video;?>" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>
Note: Don't copy anything beyond this point for your code.
Hope! it's work for you!!

PHP - Parsing iFrame from an HTML Page

Hey guys so I have a basic PHP application that loads a page with a video on it from one of those free TV streaming sites. You set the episode, season and show you wish to view and the server application parses the HTML tag of the iFrame that contains that video. The application works by parsing the HTML page with the PHP preg_match_all() method to get all occurrences of the iFrame HTML tag. I use the following string as the pattern, "/iframe .*\>/". This works for about half of the video players on the site, but for some reason comes up dry with all of the others.
For examples the video at http://www.free-tv-video-online.me/player/novamov.php?id=huv5cpp5k8cia which hosted on a video site called novamov is easily parsed. However, the video displayed at http://www.free-tv-video-online.me/player/gorillavid.php?id=8rerq4qpgsuw which is hosted on gorillavid, is not found by the preg_match_all() function despite it clearly being displayed in the HTML source when the element is inspected using chrome. Why is my script no returning the proper results and why is this behaviour dependant on the video player the video is using? Please could someone explain?
Try:
$dom = new DOMDocument; #$dom->loadHTML('yourURLHere');
$iframe = $dom->getElementsByTagName('iframe');
foreach($iframe as $ifr){
$ifrA[] = $ifr->getAttribute('src');
}
Now, the $ifrA Array should have your iframe src's.

link with same url in php

I have some thumbnail images with its larger version.I placed the thumbnail images in a page.Now for link I just gave a link
<img src="thumbnail1.jpg>
but for this I have to make different pages for showing larger one.I want to give a link to show them in a single page.means whenever I will click the thumbnail it will open the larger one in a page with the same url but with its name like
imagegallery.php?news=images/largerimage1/13.jpg
imagegallery.php?news=images/largerimage1/14.jpg
so how to do that?
Pretty basic stuff, I suggest you get to read some PHP tutorials on the internet to get some knowledge on one thing and another.
The ?news= part in your URL is a parameter that can be read by PHP. This type is known as $_GET. To get this part you would need $_GET['news'] so if we'd use your first link and place this inside a script: echo $_GET['news']; the page would say images/largerimages1/13.jpg.
In order to get the image loaded on your website we need some simple steps, I'm changing the news parameter into image, that suits better for your script since it ain't news items:
<?php
// Define the path (used to see if an image exists)
$path = 'your/absolute/path/to/public_html/'; # or wwwroot or www folder
// First check if the parameter is not empty
if($_GET['image'] != "") {
// Then check if the file is valid
if(file_exists($path . $_GET['image'])) {
// If an image exists then display image
echo '<img src="'. $_GET['image'] . '" />;
}
}
?>
Below this script you can put all your thumbnails the way you want. Ofcourse, also for these thumbnails there are some automated options. But I strongly suggest you get a good look at the script above and some beginner PHP tutorials so you completely understand the example given. This still isn't the best method, but it's kicking you in the right direction.
if your imagegallery.php is in root of your domain, you can just add slash as a first char to links like this:
<img src="thumbnail1.jpg>
else you will have to write some php function which it returns BaseUrl of your web. Then it should looks like this:
<img src="thumbnail1.jpg>
maybe you can something like this,
Techincally, there is no thumbnail image, just a stretch version of the regular image
I don't understand which part you don't know how to do:
- the link part?
it should look like
<img src="thumbnail1.jpg>
- or the PHP part (the file called imagegallery.php)?

BBcode parsing problem

Im building my own forum where you can use diffrent types of BBcode and one of them is [youtube][/youtube] to embed youtube videos,
The idea is to make it idiot-proof, the user can enter the full lenght url or just the code to the video like this
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg[/youtube]
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg&feature=related[/youtube]
[youtube]AJ3_kndmeCg[/youtube]
Here is my code that get the code out of a full lenght url,
<?php
$getpost=$_POST['post'];
$getpost=preg_replace("'\[youtube\].*?=(.*?)&.*?\[/youtube\]'is",'yt link is \\1',$getpost);
$getpost=preg_replace("'\[youtube\].*?=(.*?)\[/youtube\]'is",'yt link is \\1',$getpost);
$getpost=preg_replace("'\[youtube\](.*?)\[/youtube\]'is",'yt link is \\1',$getpost);
?>
The results are:
yt link is AJ3_kndmeCg
yt link is AJ3_kndmeCg
yt link is AJ3_kndmeCg
It works perfectly if I only want to embed 3 youtube videos,
But if i wanted to embed 6 youtube videos like this:
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg[/youtube]
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg&feature=related[/youtube] [youtube]AJ3_kndmeCg[/youtube]
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg[/youtube]
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg&feature=related[/youtube]
[youtube]AJ3_kndmeCg[/youtube]
The results are:
yt link is AJ3_kndmeCg
yt link is AJ3_kndmeCg[/youtube]
yt link is AJ3_kndmeCg
yt link is http://www.youtube.com/watch?v=AJ3_kndmeCg
[youtube]AJ3_kndmeCg
Do you see that^^ the tags still remain on two lines and one of the lines show a full lenght URL,
I'd rather use an existing BBCode parsing library like http://www.christian-seiler.de/projekte/php/bbcode/index_en.html which also allows you to define your own bb tags, and turn them into the HTML you want.
;)
Wow, this was a real toughie of a regex to write.
Got it for you in one:
$getpost=preg_replace('#\[youtube\].*?(?:v=)?([^?&[]+)(&[^[]*)?\[/youtube\]#is',
'yt link is \\1', $getpost);
This will work for all the cases you supplied. It will not work if the link begins with a part other than 'v=' (for example, 'http://www.youtube.com/watch?foo=bar&v=AJ3_kndmeCg').
Enjoy!

Categories