Php-mysql Course

This tutorial shows you a simple PHP function easy to use to convert BBCODE tags ([b], [i], [u], [block], [color], [br], [url], [img]), and character sets used for "smiles" (like: ":)", ":(", ":P", ":O", ...) to HTML.
Here is the technique:
We create two arrays inside a function, an array with regexp format for each BBCODE tag and smile-characters, and another array with HTML tags corresponding to each BBCODE, in the same order.
The function receives a string as parameter, using preg_replace(), the BBCODE tags from the first array are converted to the corresponding HTML tag defined in the second array.
- In the code bellow, under this function there is an example for test.

PHP function to convert BBCODE and smiles to HTML

<?php
// function to convert bbcode, and smiles to html
function bbcodeHtml($str) {
  // delete 'http://' because will be added when convert the code
  $str = str_replace('[url=http://', '[url=', $str);
  $str = str_replace('[url]http://', '[url]', $str);

  // Array with RegExp to recognize the code that must be converted
  $bbcode_smiles = array(
    // RegExp for [b]...[/b], [i]...[/i], [u]...[/u], [block]...[/block], [color=code]...[/color], [br]
    '/\[b\](.*?)\[\/b\]/is',
    '/\[i\](.*?)\[\/i\]/is',
    '/\[u\](.*?)\[\/u\]/is',
    '/\[block\](.*?)\[\/block\]/is',
    '/\[color=(.*?)\](.*?)\[\/color\]/is',
    '/\[br\]/is',

    // RegExp for [url=link_address]..link_name..[/url], or [url]..link_address..[/url]
    '/\[url\=(.*?)\](.*?)\[\/url\]/is',
    '/\[url\](.*?)\[\/url\]/is',

    // RegExp for [img=image_address]..image_title[/img], or [img]..image_address..[/img]
    '/\[img\=(.*?)\](.*?)\[\/img\]/is',
    '/\[img\](.*?)\[\/img\]/is',

    // RegExp for sets of characters for smiles: :), :(, :P, :P, ...
    '/:\)/i', '/:\(/i', '/:P/i', '/:S/i', '/:O/i', '/=D\>/i', '/\>:D\</i', '/:D/i', '/:-\*/i'
  );

  // Array with HTML that will replace the bbcode tags, defined inthe same order
  $html_tags = array(
    // <b>...</b>, <i>...</i>, <u>...</u>, <blockquote>...</blockquote>, <span>...</span>, <br/>
    '<b>$1</b>',
    '<i>$1</i>',
    '<u>$1</u>',
    '<blockquote>$1</blockquote>',
    '<span style="color:$1;">$2</span>',
    '<br/>',

    // a href...>...</a>, and <img />
    '<a target="_blank" href="http://$1">$2</a>',
    '<a target="_blank" href="http://$1">$1</a>',
    '<img src="$1" alt="$2" />',
    '<img src="$1" alt="$1" />',

    // The HTML to replace smiles. Here you must add the address of the images with smiles
    '<img src="icos/1.gif" alt=":)" border="0" />',
    '<img src="icos/2.gif" alt=":(" border="0" />',
    '<img src="icos/3.gif" alt=":P" border="0" />',
    '<img src="icos/4.gif" alt=":S" border="0" />',
    '<img src="icos/5.gif" alt=":O" border="0" />',
    '<img src="icos/6.gif" alt="=D&gt;" border="0" />',
    '<img src="icos/7.gif" alt="&gt;: D&lt;" border="0" />',
    '<img src="icos/8.gif" alt=": D" border="0" />',
    '<img src="icos/9.gif" alt=":-*" border="0" />'
  );

  // replace the bbcode
  $str = preg_replace($bbcode_smiles, $html_tags, $str);

  return $str;
}

// Test
$str = 'Test [b]PHP tutorials[/b].[br]
[i][color=blue]Blue, and oblique string[/color][/i], underlined: [u]BBCODE tags[/u][br]
Link: [url=https://coursesweb.net/php-mysql/]PHP Course[/url], smiles: =D>, :), >:D< ...
[block]Text added in "block", for blockquote[/block]
An image: [img=https://coursesweb.net/imgs/webcourses.gif]Course Web[/img]';

echo bbcodeHtml($str);
?>

Result:
Test PHP tutorials.
Blue, and oblique string, underlined: BBCODE tags
Link: PHP Course, smiles: =D>, :), >: D< ...
Text added in "block", for blockquote
An image: Course Web

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
Convert BBCODE and smiles to HTML

Last accessed pages

  1. Read Excel file data in PHP - PhpExcelReader (96688)
  2. Prayer The Art of Believing (1613)
  3. Convert XML to JSON in JavaScript (33940)
  4. JavaScript code and PHP (40689)
  5. SHA1 Encrypt data in JavaScript (35320)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (250)
  2. Read Excel file data in PHP - PhpExcelReader (89)
  3. The Four Agreements (73)
  4. PHP Unzipper - Extract Zip, Rar Archives (73)
  5. The Mastery of Love (66)
Chat
Chat or leave a message for the other users
Full screenInchide