So, I am using this to replace BBCode to HTML:
$text = htmlspecialchars($text);
$advanced_bbcode = array(
'#\[quote](\r\n)?(.+?)\[/quote]#si',
'#\[url](.+)\[/url]#Usi');
$advanced_html = array(
'<blockquote class="quote">$2</blockquote>',
'<a rel="nofollow" target="_blank" href="$1">$1</a>');
$text = preg_replace($advanced_bbcode, $advanced_html,$text);
echo nl2br($text);
public static function nl2br($var)
{
return str_replace(array('\\r\\n','\r\\n','r\\n','\r\n', '\n', '\r'), '<br />', nl2br($var));
}
This works fine if I only have 1 quote, but If I use multiple quotes like: [quote][quote][quote]first[/quote]second[/quote]end[/quote]
I expect to get:
<blockquote class="quote"><blockquote class="quote"><blockquote class="quote">first</blockquote>second</blockquote>end</blockquote>
But because it takes the first [/qoute] it will turn into:
<blockquote class="quote">[quote][quote]first</blockquote>second[/quote]end[/quote]
I've looked it up but I cant find anything that is working for me. I am new to this kind of stuff.
Thanks.
Make replace until there is BBCode in the string
do {
$text = preg_replace($advanced_bbcode, $advanced_html,$text,-1,$c);
} while($c);
demo
Related
I have this tag on an HTML code:
text html [button link="google.com" color="#fff" text="this text here"] rest of html
I wish i could have the parameters of this "button code" in a PHP variable, but have no idea how because of Regex.
I tried using preg_match_all but no success. Like this one:
preg_match_all('/color=(\w*)/i', $text, $color);
Thanks!
You could use preg_match_all():
<?php
$text = 'text html [button link="google.com" color="#fff" text="this text here"] rest of html';
preg_match_all('/\[button link="(.*?)" color="(.*?)" text="(.*?)"\]/i', $text, $matches);
$link = $matches[1][0];
$color = $matches[2][0];
$text = $matches[3][0];
echo $link;
echo $color;
echo $text;
Output:
google.com
#fff
this text here
Thanks guys. I will post the result that worked for me. Maybe it helps someone else! The options are already in my language, portuguese. But you can understand!
I needed the script to solve the codes for more than one tag in the same HTML.
$matches = [];
preg_match_all('/\[botao link={(.*?)} texto={(.*?)} corfundo={(.*?)} corletra={(.*?)}\]/', $text, $matches);
foreach($matches[0] as $key => $match) {
$link = $matches[1][$key];
$texto = $matches[2][$key];
$corfundo = $matches[3][$key];
$corletra = $matches[4][$key];
$text = str_replace($match, '<a target="_blank" title="'.$texto.'" class="botao" href="'.$link.'" style="background-color: '.$corfundo.'; color: '.$corletra.';">'.$texto.'</a>', $text);
}
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>
What I want
If the URL in the string contains a .jpg at the end of the URL (not the string) then it should make an image from it with preg_replace else make a normal link.
so for example:
If I have http://www.example.com/images/photo.jpg then it should replace with:
<img src="http://www.example.com/images/photo.jpg" alt="http://www.example.com/images/photo.jpg">
The problem:
The URL is replaced with a link in any way and my regex isn't working :( .
What I have tried:
$content = preg_replace("/(http:\/\/[^\s]+(?=\.jpg))/i","<img src=\"$1\" alt = \"$1\"></img>",$content);
$content = nl2br(preg_replace("/(http:\/\/[^\s]+(?!\.jpg))/m", "$1", $content));
Try this
function replace_links($content)
{
if (preg_match('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $content))
{
$content = preg_replace('#(http://[^\s]+(?=\.(jpe?g|png|gif)))(\.(jpe?g|png|gif))#i', '<img src="$1.$2" alt="$1.$2" />', $content);
}
else
{
$content = preg_replace('#(http://[^\s]+(?!\.(jpe?g|png|gif)))#i', '$1', $content);
}
return $content;
}
$content = preg_replace('#\b(http://\S+\.jpg)\b#i', '<img src="$1" alt="$1" />', $content);
You don't need lookaround. Just go with
$content = preg_replace("#(http://[^ ]+\\.jpg(?= |$)#i","<img src=\"$1\" alt=\"$1\"/>", $content);
I think you used the lookahead operator when you wanted lookbehind. You could change (?=\.jpg) to (?<=\.jpg) but there are other, cleaner regex's I'm sure others will post.
This worked for me.
$parse_img='Hello, http://orbitco-ccna-pastquestions.com/images/Q5.jpg
In the figure above, router R1 has two point-to-point . ';
$parse_img=preg_replace('/(https?:\/\/(.\*)?\\.jpg|png|gif)[\s+]*/i',"< img src=\"$1\" alt = \"$1\">< /img >",$parse_img);
echo $parse_img;
Suyash
When I run my parsing, it breaks images to replace the URLs with links through the Oembed system. How can I edit this Regex so it will not capture links within BBCode brackets?
// Parse bbcodes before link parsing for image support
$text = self::parseBBCodes($text);
$text = preg_replace_callback('/(https?:\/\/.*?)(\s|$)/i', function ($match) use (&$oembedCount, &$maxOembedCount) {
I have now tried
$text = preg_replace_callback('/(?<!\])(https?:\/\/.*?)(\s|$)(?!\[)/i', function ($match) use (&$oembedCount, &$maxOembedCount) {
Which seems works, but images are not being converter. Though regular bbcode is.
BBCode Function
/**
* Parse BBCode
*
* #param string $text contains the text with BBCode to be parsed
*/
public static function parseBBcodes($text)
{
// BBcode array
$find = array(
'~\[b\](.*?)\[/b\]~s',
'~\[i\](.*?)\[/i\]~s',
'~\[u\](.*?)\[/u\]~s',
'~\[quote\](.*?)\[/quote\]~s',
'~\[size=(.*?)\](.*?)\[/size\]~s',
'~\[color=(.*?)\](.*?)\[/color\]~s',
'~\[img\](https?://.*?\.(?:jpg|jpeg|gif|png|bmp))\[/img\]~s'
);
// HTML tags to replace BBcode
$replace = array(
'<b>$1</b>',
'<i>$1</i>',
'<span style="text-decoration:underline;">$1</span>',
'<pre>$1</'.'pre>',
'<span style="font-size:$1px;">$2</span>',
'<span style="color:$1;">$2</span>',
'<img src="$1" alt="" />'
);
// Replacing the BBcodes with corresponding HTML tags
return preg_replace($find,$replace,$text);
}
you may want to strip your text
$text= strip_tags($text);
I'm working on a parser for my site and I've run into an issue--Is there anyway to remove/insert "http://" from a string user preg_replace? I know the basics of how to use the function, and have been following tutorials, but I'm not exactly sure how to go about doing this.
here's my code:
function showBBcodes($text)
{
// BBcode array
$find = array(
'~\[b\](.*?)\[/b\]~s',
'~\[i\](.*?)\[/i\]~s',
'~\[u\](.*?)\[/u\]~s',
'~\[quote\](.*?)\[/quote\]~s',
'~\[size=(.*?)\](.*?)\[/size\]~s',
'~\[color=(.*?)\](.*?)\[/color\]~s',
'~\[url=([^]]*)\]([^[]*)\[/url\]~s',
'~\[img\](https?://.*?\.(?:jpg|jpeg|gif|png|bmp))\[/img\]~s'
);
// HTML tags to replace BBcode
$replace = array(
'<b>$1</b>',
'<i>$1</i>',
'<span style="text-decoration:underline;">$1</span>',
'<pre>$1</'.'pre>',
'<span style="font-size:$1px;">$2</span>',
'<span style="color:$1;">$2</span>',
'$2\',
'<img src="$1" alt="" />'
);
// Replacing the BBcodes with corresponding HTML tags
return preg_replace($find,$replace,$text);
}