Replace YouTube and Vimeo embeds with their poster images

Completed Posted 7 years ago Paid on delivery
Completed Paid on delivery

Given some html I want a php function to find all youtube and vimeo videos contained with [embed] tags and replace them with the their corresponding poster images.

To this point I have created some code but I can't work out a few issues see below.

The problems that I am having are:

- If [embed] tags are on one line the first preg_replace works but overwrites the others

- Also I can't work out how to get the poster image for vimeo videos

If you paste this code into phpfiddle and run it you will see what I mean

$embeds_on_1_line = '[embed][url removed, login to view][/embed] [embed][url removed, login to view][/embed] [embed][url removed, login to view][/embed]<br><h2>ABOVE see - Embeds on one line doesn\'t work Vimeo videos missing</h2>';

$html1= replace_sc_with_images($embeds_on_1_line);

echo $html1;

$embeds_on_multi_line = '[embed][url removed, login to view][/embed]

[embed][url removed, login to view][/embed]

[embed][url removed, login to view][/embed]<br><h2>Multi line works</h2>';

$html2= replace_sc_with_images($embeds_on_multi_line);

echo $html2;

function replace_sc_with_images($html) {

$temp_vimeo_image = '[url removed, login to view]';

$html_back = '';

//replace youtube

$html_back = preg_replace('#\[embed\]((?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=))([\w-]{10,12}))(.*)?\[\/embed\]#', '<a href="$1"><img src="[url removed, login to view]$2/[url removed, login to view]"></a>', $html);

//replace vimeo

$html_back = preg_replace('#\[embed\](https?:\/\/(?:www\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11}))\[\/embed\]#', '<a href="$1"><img src="'.$temp_vimeo_image.'"></a>', $html_back);

return $html_back;

}

PHP Regular Expressions

Project ID: #10576663

About the project

2 proposals Remote project Active 7 years ago

Awarded to:

$25 AUD in 10 days
(0 Reviews)
0.0

2 freelancers are bidding on average $25 for this job

akhilpaul009

A proposal has not yet been provided

$25 AUD in 0 days
(0 Reviews)
0.0