bbCode, adding smileys? - php

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!
*/

Related

How can i get image using preg_match_all

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

Call Function PREG_REPLACE

With security and also a deprecated function; what would be the easiest and most secure way to call a function in a find and replace?
There are four find and replace modules which can be inserted within content [album][/album], [img][/img], [youtube][/youtube], or [vimeo][/vimeo].
Using the function I put together so far Images, YouTube and Vimeo were a no brainer. The Album no so much. I would like to call a function based on parameters that are passed.
I tried altering this function into a preg_replace_callback and that just mocks up everything. Is there any alternatives?
function FormatModules($text) {
$find = array(
'~\[album\](.+?)\[/album\]~s',
'~\[img width=(.*?) height=(.*?) alt=(.*?)\](https?://.*?\.(?:jpg|jpeg|gif|png))\[/img\]~s',
'~\[youtube\](.+?)\[/youtube\]~s',
'~\[vimeo\](.+?)\[/vimeo\]~s'
);
$replace = array(
'GenerateAlbum($1)', // call a PHP function
'<img src="$4" width="$1" height="$2" alt="$3" />',
'<iframe src="http://www.youtube.com/embed/$1"></iframe>',
'<iframe src="https://player.vimeo.com/video/$1"></iframe>'
);
return preg_replace($find, $replace, $text);
}
If you wanted to call a function on more than one replacement or you wish to set up your script for future modifications so that functions can be called on the replacement parameter, you might entertain preg_replace_callback_array().
Otherwise, I'd say make a preg_replace_callback() involving the first elements of $find and $replace then run a call of preg_replace() on the remaining elements.
Code: (Demo)
function GenerateAlbum($match) {
return "<div class=\"album\>Do whatever: " . strtoupper($match[1]) . "</div>";
}
function FormatModules($text) {
$text = preg_replace_callback('~\[album\](.+?)\[/album\]~s', "GenerateAlbum", $text);
$find = array(
'~\[img width=(.*?) height=(.*?) alt=(.*?)\](https?://.*?\.(?:jpg|jpeg|gif|png))\[/img\]~s',
'~\[youtube\](.+?)\[/youtube\]~s',
'~\[vimeo\](.+?)\[/vimeo\]~s'
);
$replace = array(
'<img src="$4" width="$1" height="$2" alt="$3" />',
'<iframe src="http://www.youtube.com/embed/$1"></iframe>',
'<iframe src="https://player.vimeo.com/video/$1"></iframe>'
);
return preg_replace($find, $replace, $text);
}
echo FormatModules("[vimeo]test1[/vimeo]\n\n[album]test2[/album]\n\njust text\n\n[img width=50 height=100 alt='sumpin good']http://www.example.com/image.gif[/img]\n\n[youtube]test3[/youtube]");
Output:
<iframe src="https://player.vimeo.com/video/test1"></iframe>
<div class="album\>Do whatever: TEST2</div>
just text
<img src="http://www.example.com/image.gif" width="50" height="100" alt="'sumpin good'" />
<iframe src="http://www.youtube.com/embed/test3"></iframe>

Regex to remove links surrounding images

I have a page of images wrapped with links. Essentially I want to remove the links surrounding images, but keep the image tags in tact.
eg. I have:
<img src="image1.jpg" alt="image 1">
and I want:
<img src="image1.jpg" alt="image 1">
I tried this code I found in my research, but it leaves a stray </a> tag.
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
'{ wp-image-[0-9]*" ></a>}'),
array('<img','" />'),
$content
);
I have no idea when it comes to regular expressions so can someone please fix my code? :-)
You can use <a.*?(<img.*?>)<\/a> to match and replace with $1
See DEMO
$content = preg_replace('/<a.*?(<img.*?>)<\/a>/', '$1', $content);
by your provided regex it seems you are using wordpress and want to remove hyperlinks from content.
if you are using wordpress then you can also use this hook to remove hyper links on images from content.
add_filter( 'the_content', 'attachment_image_link_remove_filter' );
function attachment_image_link_remove_filter( $content ) {
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
'{ wp-image-[0-9]*" /></a>}'),
array('<img','" />'),
$content
);
return $content;
}
here is the another function that will also work.
function attachment_image_link_remove_filter($content)
{
$content =
preg_replace(array('{<a[^>]*><img}', '{/></a>}'), array('<img', '/>'), $content);
return $content;
}
add_filter('the_content', 'attachment_image_link_remove_filter');
OR you can use also Demo
$string = '<img src="image1.jpg" alt="image 1">';
$result = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $string);
echo $result; // this will output "<img src="image1.jpg" alt="image 1">"
I think that
$content = preg_replace('/<a\s+href=[^>]+>(<img[^>]+>)<\/a>/', '$1', $content);
is a better solution.
Ex : https://regex101.com/r/3ozruM/1
Because #karthik manchala's solution https://regex101.com/r/ETkE58/1 doesn't work in such a case.

PHP : Getting image by removing all classes, ids ..ect from source

I have a image in database stored in this format
$link = this is my photo <img data-liked='0' data-reblogged='0' data-attachment-id="299971" data-medium-file="http://my.files.wordpress.com/2013/12/img_6697.png?w=394" width="73" height="130" src="http://my.files.wordpress.com/2013/12/img_6697.png?w=73&h=130" class="attachment-thumbnail" alt="IMG_6697" />, school photo;
i was wondring if this is possible to just get output
this is my photo <img src="http://my.files.wordpress.com/2013/12/img_6697.png?w=73&h=130" />, school photo
Just image source nothing else ...
I have tryed this and got image url but i need above result
$str = preg_replace('#<img.+?src=[\'"]([^\'"]+)[\'"].+/>#i', "$1", $link);
echo $str;
$str = preg_replace('#<img.*?src="(.*?)".*?\/>#i', "<img src=\"$1\" />", $link);
echo $str;
You might want to keep the alt:
$str = preg_replace('#<img.*?src="(.*?)".*?alt="(.*?)".*?\/>#i', "<img src=\"$1\" alt=\"$2\" />", $link);
echo $str;

PHP: change [img]src[/img] to <img src="src" alt="src" > with regular expression

I want to change [img]src[/img] to <img src="src" alt="src" > with regular expression.
I found some examples which convert <img src=""> to [img][/img] but not my case.
Thank you!
I beleive this article will help you with your problem...
http://thesinkfiles.hubpages.com/hub/Regex-for-BBCode-in-PHP
function parseCode($txt)
{
// these functions will clean the code first
$ret = strip_tags($txt);
// code replacements
$ret = preg_replace('#\[b\](.+)\[\/b\]#iUs', '<b>$1</b>', $ret);
$ret = preg_replace('#\[link\=(.+)\](.+)\[\/link\]#iUs', '$2', $ret);
$ret = preg_replace('#\[img\](.+)\[\/img\]#iUs', '<img src="$1" alt="Image" />', $ret);
$ret = preg_replace('#\[quote\=(.+)\](.+)\[\/quote]#iUs', '<div class="quote">$2</div><div class="quote-by">By: $1</div>', $ret);
// return parsed string
return $ret;
}
$ret = preg_replace('#\[img\](.+)\[\/img\]#iUs', '<img src="$1" alt="img">', $ret);
But in general you want something like a dedicated phpBB script, or phpBB class.
Even PHP itself got BBCode text processor: http://www.php.net/manual/en/book.bbcode.php
You can test it
$str= preg_replace('~\[img\](.*)\[\/img\]~si', '<img src="$1" alt="$1">', $str);

Categories