I already a bbcode string $mybbcode = [b]Hello word[/b] with php i want to show it with html format in html page.
ex: <div><b>hello word</b><div>
Basically that others already said to you after, but if you search in Google you'll see quicky lot of info about that, and done functions. Here is a sample:
function bbc2html($content) {
$search = array (
'/(\[b\])(.*?)(\[\/b\])/',
'/(\[i\])(.*?)(\[\/i\])/',
'/(\[u\])(.*?)(\[\/u\])/',
'/(\[ul\])(.*?)(\[\/ul\])/',
'/(\[li\])(.*?)(\[\/li\])/',
'/(\[url=)(.*?)(\])(.*?)(\[\/url\])/',
'/(\[url\])(.*?)(\[\/url\])/'
);
$replace = array (
'<strong>$2</strong>',
'<em>$2</em>',
'<u>$2</u>',
'<ul>$2</ul>',
'<li>$2</li>',
'$4',
'$2'
);
return preg_replace($search, $replace, $content);
}
Only for lazy programmers ;)
I invite you to search and decide what are the best from all code already done for you project.
You will have to use regex to convert BBCodes to HTML : http://www.php.net/manual/en/ref.pcre.php
For example :
$string = preg_replace('#\[b\](.+)\[\/b\]#iUs', '<b>$1</b>', $string);
Related
I have a data base with texts and in each text there are words (tags) that start with # (example of a record : "Hi I'm posting an #issue on #Stackoverflow ")
I'm trying to find a solution to add html code to transform each tag into a link when printing the text.
So the text are stored as strings in MySQL database like this :
Some text #tag1 text #tag2 ...
I want to replace all these #abcd with
#abcd
And have a final result as follow:
Some text #tag1 text #tag2 ...
I guess that i should use some regex but it is not at all my strong side.
Try the following using preg_replace(..)
$input = "Hi I'm posting an #issue on #Stackoverflow";
echo preg_replace("/#([a-zA-Z0-9]+)/", "<a href='targetpage.php?val=$1'>#$1</a>", $input);
http://php.net/manual/en/function.preg-replace.php
A simple solution could look like this:
$re = '/\S*#(\[[^\]]+\]|\S+)/m';
$str = 'Some text #tag1 text #tag2 ...';
$subst = '#$1';
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Demo
If you are actually after Twitter hashtags and want to go crazy take a look here how it is done in Java.
There is also a JavaScript Twitter library that makes things very easy.
Try this the function
<?php
$demoString1 = "THIS is #test STRING WITH #abcd";
$demoString2 = "Hi I'm posting an #issue on #Stackoverflow";
function wrapWithAnchor($link,$string){
$pattern = "/#([a-zA-Z0-9]+)/";
$replace_with = '<a href="'.$link.'?val=$1">$1<a>';
return preg_replace( $pattern, $replace_with ,$string );
}
$link= 'http://www.targetpage.php';
echo wrapWithAnchor($link,$demoString1);
echo '<hr />';
echo wrapWithAnchor($link,$demoString2);
?>
I have a link being outputted on my site, what i want to do is replace the visible text that the user sees, but the link will always remain the same.
There will be many different dynamic urls with the text being changed, so all the example regex that i have found so far only use exact tags like '/.*/'...or something similar
Edited for a better example
$link = '<a href='some-dynamic-link'>Text to replace</a>';
$pattern = '/#(<a.*?>).*?(</a>)#/';
$new_text = 'New text';
$new_link = preg_replace($pattern, $new_text, $link);
When printing the output, the following is what i am looking for, against my result.
Desired
<a href='some-dynamic-link'>New text</a>
Actual
'New text'
As you're already using the capture groups, why not actually use them.
$link = "<a href='some-dynamic-link'>Text to replace</a>";
$newText = "Replaced!";
$result = preg_replace('/(<a.*?>).*?(<\/a>)/', '$1'.$newText.'$2', $link);
If you needs to get everything in Between tags then you can use below function
<?php
function getEverything_inBetween_tags(string $htmlStr, string $tagname)
{
$pattern = "#<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s";
preg_match_all($pattern, $htmlStr, $matches);
return $matches[1];
}
$str = 'see here for more details about test.com';
echo getEverything_inBetween_tags($str, 'a');
//output:- see here for more details about test.com
?>
if you needs to extract HTML Tag & get Array of that tag
<?php
function extractHtmlTag_into_array(string $htmlStr, string $tagname)
{
preg_match_all("#<\s*?$tagname\b[^>]*>.*?</$tagname\b[^>]*>#s", $htmlStr , $matches);
return $matches[0];
}
$str = '<p>test</p>test.com<span>testing string</span>';
$res = extractHtmlTag_into_array($str, 'a');
print_r($res);
//output:- Array([0] => "amazon.in/xyz/abc?test=abc")
?>
I have simple BBCode parser:
function parse($text) {
$text = htmlspecialchars($text);
$text = nl2br($text);
$text = preg_replace("#\[b\](.*?)\[/b\]#si", '<b>\\1</b>', $text);
$text = preg_replace("#\[i\](.*?)\[/i\]#si", '<i>\\1</i>', $text);
$text = preg_replace("#\[u\](.*?)\[/u\]#si", '<u>\\1</u>', $text);
$text = preg_replace("#\[color=(.*?)\](.*?)\[/color\]#si", "<span style=\"color:\\1;\">\\2</span>", $text);
//and some more rules [...]
return $text;
}
It work's good when i have simple input, but when user trying use color in color, it's not working.
For example 1:
[b]bold[color=#f00]red[/color][i]italic[/i][/b]
everything is OK, but when user try something like example 2:
[b]bold[color=#f00]red[color=#0f0]green[/color][/color][i]italic[/i][/b]
my function returns:
<b>bold<span style="color:#f00;">red[color=#0f0]green</span>[/color]<i>italic</i></b>
of course example 3 working good:
[b]bold[color=#f00]red[/color][color=#0f0]green[/color][i]italic[/i][/b]
My question is it any simple solution to build something like DOM and then parse expresion?
I'd like get something like this for 2nd example:
<b>bold<span style="color:#f00;">red<span style="color:#0f0;">green</span></span><i>italic</i></b>
You should look into already existing solutions if you're willing to parse complex BBCode (see the post mario linked in a comment for reference).
However, if you're willing to stick with your own implementation, you can use recursive regexes, for example this way:
<?php
function bbcodeColor($input)
{
$regex = '#\[color=(.*?)\](((?R)|.)*?)\[\/color\]#is';
if (is_array($input)) {
$input = '<span style="color:'.$input[1].';">'.$input[2].'</span>';
}
return preg_replace_callback($regex, 'bbcodeColor', $input);
}
echo bbcodeColor('[color=#f00]red[color=#0f0]green[/color][/color]');
// <span style="color:#f00;">red<span style="color:#0f0;">green</span></span>
Hi all I have a very simple bbcode parsing system, it is currently having problems with lists within lists.
My code:
$find = array(
'/\[list\](.*?)\[\/list\]/is',
'/\[\*\](.*?)(\n|\r\n?)/is',
'/\[ul\](.*?)\[\/ul\]/is',
'/\[li\](.*?)\[\/li\]/is'
);
$replace = array(
'<ul>$1</ul>',
'<li>$1</li>',
'<ul>$1</ul>',
'<li>$1</li>'
);
$body = preg_replace($find, $replace, $body);
The problem is when you have another list inside the li tags it then completely fails to parse, screenshot showing:
This is how it should look:
I know my code is probably too simple for it but how do i adjust it so it can parse a list within a list item?
Rather than using Regular Expressions you have a couple of options..
Use PHP's BBCode Parsing extension
Do a much simpler replacement, ie. straight up replace [ul] with <ul> etc.
I'm not saying it can't be done with Regex, just that it's not the simplest option.
Here's a still-regex based replacement:
$body = '[ul][li]test[/li][li]test[/li][li]test[ul][li]lol[/li][/ul][/li][li]hehe[/li][/ul]';
$find = array(
'/\[(\/?)list\]/i',
'/\[\*\](.*?)(\n|\r\n?)/i',
'/\[(\/?)ul\]/i',
'/\[(\/?)li\]/i'
);
$replace = array(
'<$1ul>',
'<li>$1</li>',
'<$1ul>',
'<$1li>'
);
$body = preg_replace($find, $replace, $body);
Alright so I am using a little bbcode function for a forum I have, working well, so if, in example, I put
[b]Text[/b]
it will print Text in bold.
My issue is, if I have that code:
[b]
Text[/b]
Well it will not work, and just print that as it's right now.
Here is an example of the function I am using:
function BBCode ($string) {
$search = array(
'#\[b\](.*?)\[/b\]#',
);
$replace = array(
'<b>\\1</b>',
);
return preg_replace($search , $replace, $string);
}
Then when echo'ing it:
.nl2br(stripslashes(BBCode($arr_thread_row[main_content]))).
So my question would be, what is necessary so the BBcode works with everything inside it, but no necessarily on the same line.
In example:
[b]
Text
[/b]
Would simply be
Text
Thank you for any help!
Alex
You need the multiline modifier, which makes your pattern something like #\[b\](.*?)\[/b\]#ms
(note the trailing m)
There is actually a pecl extension that parses BBcode, which would be faster and more secure than writing it from scratch yourself.
I use this... It should work.
$bb1 = array(
"/\[url\](.*?)\[\/url\]/is",
"/\[img\](.*?)\[\/img\]/is",
"/\[img\=(.*?)\](.*?)\[\/img\]/is",
"/\[url\=(.*?)\](.*?)\[\/url\]/is",
"/\[red\](.*?)\[\/red\]/is",
"/\[b\](.*?)\[\/b\]/is",
"/\[h(.*?)\](.*?)\[\/h(.*?)\]/is",
"/\[php\](.*?)\[\/php\]/is"
);
$bb2 = array(
'\\1',
'<img alt="" src="\\1"/>',
'<img alt="" class="\\1" src="\\2"/>',
'<a rel="nofollow" target="_blank" href="\\1">\\2</a>',
'<span style="color:#ff0000;">\\1</span>',
'<span style="font-weight:bold;">\\1</span>',
'<h\\1>\\2</h\\3>',
'<pre><code class="php">\\1</code></pre>'
);
$html = preg_replace($bb1, $bb2, $html);