Can someone tell me why when I run this code the preg_replace function seems like it does nothing?
<?php
$string = 'waka http://video.webmfiles.org/big-buck-bunny_trailer.webm waka';
$search = '#http\:\/\/.\.webm #';
$replace = '<video width="320" height="240" controls><source src="$1" type="video/webm"></video>';
$url = preg_replace($search,$replace,$string);
echo $url;
?>
Is my $search string wrong? If so, how can I fix it? It's suppose to replace strings starting in http:// and ending in .webm and surround them with the html code needed to play the .webm video.
Here's how I'd do this...
$string = 'waka http://video.webmfiles.org/big-buck-bunny_trailer.webm waka';
$search = '/(https?\:\/\/.+?\.webm)\h/';
$replace = '<video width="320" height="240" controls><source src="$1" type="video/webm"></video> ';
$url = preg_replace($search,$replace,$string);
echo $url;
Output:
waka <video width="320" height="240" controls><source src="http://video.webmfiles.org/big-buck-bunny_trailer.webm" type="video/webm"></video> waka
Regex101 demo: https://regex101.com/r/qR1xJ7/2
I ended up figuring it out. I'm now using:
$search = '#http:\/\/([^\']+)\.webm #';
Related
i need some help for get images. Im using preg_match_all function.
Source: <img alt="test" title="test" src="/data/brands/test.png">
how can i get full image url ?
And this is my code for text. I need add image here.
<?
$link = 'link';
$marka = '#<div class="test">(.*?)</div>#si';
$getir = file_get_contents($link);
preg_match_all($marka,$getir,$test1);
$test = $test1[0];
echo $test[0]; ?>
Thanks.
This might help you
$str = '<img alt="test" title="test" src="/data/brands/test.png">';
$regex = '#src="(.+?)">#';
preg_match($regex,$str,$match);
echo $match[1];
//prints: /data/brands/test.png
Hello everyone i am facing a problem to explode a MwLAI_TXowc a from a specific string,
I want to explode MwLAI_TXowc from following youtube code.
<iframe width="640" height="360" src="https://www.youtube.com/embed/MwLAI_TXowc" frameborder="0" allowfullscreen=""></iframe>
Please help me how to explode MwLAI_TXowc from youtube embed code in php.
Thanks in advance
You dont have to use PHP function explode for this PHP function pathinfo is much easier.
<?php
$url = 'https://www.youtube.com/embed/MwLAI_TXowc';
$basename = pathinfo($url)['basename'];
echo '<pre>';
var_dump($basename);
echo '<pre>';
?>
Output
string(11) "MwLAI_TXowc"
Or if your PHP doesnt support direct array access with functions
<?php
$url = 'https://www.youtube.com/embed/MwLAI_TXowc';
$pathinfo = pathinfo($url);
$basename = $pathinfo['basename'];
echo '<pre>';
var_dump($basename);
echo '<pre>';
?>
Output
string(11) "MwLAI_TXowc"
Use preg_match for your string
$s = '<iframe width="640" height="360" src="https://www.youtube.com/embed/MwLAI_TXowc" frameborder="0" allowfullscreen=""></iframe>';
preg_match('/.*"http.*\/embed\/(.*?)".*/', $s, $matches);
var_dump($matches);
So I'm trying to add smileys to my website (bbCodes) but I can't figure out how to do it. I have all the smileys triggerwords and outputs in my database to make it easier to remove/add smileys.
This bit of code below does nothing... I do not get an error and it doesn't replace for example :happy: with the image happy.png
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('/var/www/files/connect.php');
$SmileysQ = $DB->query("SELECT * FROM smileys");
$SmileysQ->setFetchMode(PDO::FETCH_ASSOC);
while($Smileys = $SmileysQ->fetch()) {
$text = preg_replace ('/\''.$Smileys['trigger'].'/is', '<img src="images/smileys/'.$Smileys['output'].'.png" height="15" width="15" />', $text);
}
What have I done wrong?
I think you have an unintended ' mark in the first argument of the preg_replace causing it to fail as it was searching for ':happy: not :happy:
The proper replace would more likely be:
$text = preg_replace ('/'.$Smileys['trigger'].'/is', '<img src="images/smileys/'.$Smileys['output'].'.png" height="15" width="15" />', $text);
example:
$text = ":happy: this is a test!";
$code = ":happy:";
$text = preg_replace ('/'.$code.'/is', '<img src="images/smileys/happy.png" height="15" width="15" />', $text);
print $text;
/*
outputs: <img src="images/smileys/happy.png" height="15" width="15" /> this is a test!
the extra ' gave me: :happy: this is a test!
*/
I have tried to strip the code below
<iframe width="560" height="315" src="//www.youtube.com/embed/QUVdnzC19oU" frameborder="0" allowfullscreen></iframe>
its working but not stripping the links properly all i get is
http://img.youtube.com/vi/www.youtube.comembedQUVdnzC19oU/0.jpg
instead of
http://img.youtube.com/vi/QUVdnzC19oU/0.jpg
Please find the php code below.
if(preg_match_all('#<iframe\s[^>]*src=[\"|\']([^\"\'\>]+)[^>].*?</iframe>#siu', $item->introtext, $iframesrc) >0){
if(isset($iframesrc[1])){
$vid = str_replace(
array(
'http://youtu.be/',
'http://www.youtube.com/embed/',
'http://youtube.googleapis.com/v/'), '', $iframesrc[1][0]);
$vid = preg_replace('#\/.*?#i', '', $vid);
if(!(empty($vid))){
$result = '' .
'<div class="vimage">
<a class="video-link vlink" href="'.$item->link.'" title="">
<img src="http://img.youtube.com/vi/'.$vid.'/0.jpg" />
<span class="play-icon"> </span>
</a>
</div>';
$item->introtext = str_replace($iframesrc['0'], '', $item->introtext);
}
}
}
Thanks in advance.
Replace
array(
'http://youtu.be/',
'http://www.youtube.com/embed/',
'http://youtube.googleapis.com/v/'), '', $iframesrc[1][0]);
by
array(
'//youtu.be/',
'//www.youtube.com/embed/',
'//youtube.googleapis.com/v/',
'http:', 'https:'), '', $iframesrc[1][0]);
The problem is that you URL das not start with http:// as your code expects. My solution will work with //, http:// and https://.
Also the following line is not useful, as it will never have any effect.
$vid = preg_replace('#\/.*?#i', '', $vid);
<?php
$url = "www.youtube.com/embed/QUVdnzC19oU";
$urltokens = explode("/", $url);
$vid_id = $urltokens[2];
echo "http://img.youtube.com/vi/" . $vid_id . "/0.jpg";
?>
Considering you don't have http:// in the middle, this code will work.
This is what I've got so far:
<?php
$content = "word1 http://www.youtube.com/watch?v=yqfKe-67foQ&feature=related word2 http://www.youtube.com/watch?v=2vq7gDEn99Y&feature=related word3 http://www.youtube.com/watch?v=nW5HxgMYRto\nhttp://www.youtube.com/watch?v=8Uc2lpH0iZ0&feature=fvhl";
$pattern = '/http:\/\/www\.youtube\.com\/watch\?(.*)v=([a-zA-Z0-9_\-]+)(\S*)/i';
$replace = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/$2&hl=en_US&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$2&hl=en_US&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';
$content = preg_replace($pattern, $replace, $content);
echo $content;
?>
I honestly have no idea why it doesn't work. Some help would be appreciated. Thanks.
You just have to make * non-greedy:
http:\/\/www\.youtube\.com\/watch\?(.*?)v=([a-zA-Z0-9_\-]+)(\S*)
See "Watch out for greediness".
Try this simple function for URL replacement
function youtube($string)
{
return preg_replace(
'#(http://(www.)?youtube.com)?/(v/|watch\?v\=)([-|~_0-9A-Za-z]+)&?.*?#i',
'<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/$4" frameborder="0" allowfullscreen></iframe>',
$string
);
}
echo youtube('http://www.youtube.com/watch?v=VWsjWCt1PsQ');
echo youtube('http://youtube.com/watch?v=VWsjWCt1PsQ');
echo youtube('http://youtube.com/v/VWsjWCt1PsQ');
echo youtube('http://www.youtube.com/v/VWsjWCt1PsQ');
I would just create a string with a configured embed code. Link attribute could be #LINK#, width="#WIDTH#". Than replace all parameters with your own value. It could be much more comfortable to use. Or you can simply use a youtube embed code generator for that.