Skip to content

Instantly share code, notes, and snippets.

@Eyad-Bereh
Created August 8, 2019 21:29
Show Gist options
  • Save Eyad-Bereh/31ea8d218205a8404911991dfa60502e to your computer and use it in GitHub Desktop.
Save Eyad-Bereh/31ea8d218205a8404911991dfa60502e to your computer and use it in GitHub Desktop.
A function to convert some special formatting codes into their equivalents in HTML
<?php
if (!function_exists("StringToHTML")) {
function StringToHTML($string) {
// The following regular expression was originally formed by Diego Perini
// Take a look at it here: https://gist.github.com/dperini/729294
$LinkRegex = "<\[url\](?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?\[\/url\]>u";
$string = preg_replace_callback($LinkRegex, function($matches) {
$match = preg_replace("<\[url\]|\[\/url\]>", "", $matches[0]);
return "<a href='" . $match . "' target='_blank'>" . $match ."</a>";
}, $string);
$YoutubeRegex = "<\[youtube\](https?:\/\/(www\.)?youtube\.com\/watch\?([-a-zA-Z0-9()@:%_\+.~#?&//=]+))\[\/youtube\]>u";
$string = preg_replace_callback($YoutubeRegex, function($matches) {
$id = "";
$parameters = explode("?", $matches[0])[1];
$parameters = preg_replace("<\[\/youtube\]>", "", $parameters);
$parameters = explode("&", $parameters);
for ($i = 0; $i < count($parameters); $i++) {
$keyvalue = explode("=", $parameters[$i]);
if ($keyvalue[0] === "v") {
$id = $keyvalue[1];
}
}
$match = preg_replace("<\[youtube\]|\[\/youtube\]>", "", $matches[0]);
return "<div class='embed-responsive embed-responsive-16by9'><iframe src='https://www.youtube.com/embed/" . $id . "' class='embed-responsive-item border border-0'></iframe></div>";
}, $string);
$string = str_ireplace("\n", "<br/>", $string);
$string = str_ireplace("\r", "<br/>", $string);
$string = str_ireplace("\r\n", "<br/>", $string);
$BoldRegex = "/\[bold\](.*?)\[\/bold\]/";
$string = preg_replace_callback($BoldRegex, function($matches) {
$temp = preg_replace("/\[bold\]|\[\/bold\]/", "", $matches[0]);
return "<b>" . $temp . "</b>";
}, $string);
$ItalicRegex = "/\[italic\](.*?)\[\/italic\]/";
$string = preg_replace_callback($ItalicRegex, function($matches) {
$temp = preg_replace("/\[italic\]|\[\/italic\]/", "", $matches[0]);
return "<i>" . $temp . "</i>";
}, $string);
$UnderlinedRegex = "/\[underlined\](.*?)\[\/underlined\]/";
$string = preg_replace_callback($UnderlinedRegex, function($matches) {
$temp = preg_replace("/\[underlined\]|\[\/underlined\]/", "", $matches[0]);
return "<u>" . $temp . "</u>";
}, $string);
$CenterRegex = "/\[center\](.*?)\[\/center\]/";
$string = preg_replace_callback($CenterRegex, function($matches) {
$temp = preg_replace("/\[center\]|\[\/center\]/", "", $matches[0]);
return "<div class='text-center'>" . $temp . "</div>";
}, $string);
$RightRegex = "/\[right\](.*?)\[\/right\]/";
$string = preg_replace_callback($RightRegex, function($matches) {
$temp = preg_replace("/\[right\]|\[\/right\]/", "", $matches[0]);
return "<div class='text-right'>" . $temp . "</div>";
}, $string);
$LeftRegex = "/\[left\](.*?)\[\/left\]/";
$string = preg_replace_callback($LeftRegex, function($matches) {
$temp = preg_replace("/\[left\]|\[\/left\]/", "", $matches[0]);
return "<div class='text-left'>" . $temp . "</div>";
}, $string);
$UppercaseRegex = "/\[uppercase\](.*?)\[\/uppercase\]/";
$string = preg_replace_callback($UppercaseRegex, function($matches) {
$temp = preg_replace("/\[uppercase\]|\[\/uppercase\]/", "", $matches[0]);
return "<span class='text-uppercase'>" . $temp . "</span>";
}, $string);
$LowercaseRegex = "/\[lowercase\](.*?)\[\/lowercase\]/";
$string = preg_replace_callback($LowercaseRegex, function($matches) {
$temp = preg_replace("/\[lowercase\]|\[\/lowercase\]/", "", $matches[0]);
return "<span class='text-lowercase'>" . $temp . "</span>";
}, $string);
$CapitalizedRegex = "/\[capitalized\](.*?)\[\/capitalized\]/";
$string = preg_replace_callback($CapitalizedRegex, function($matches) {
$temp = preg_replace("/\[capitalized\]|\[\/capitalized\]/", "", $matches[0]);
return "<span class='text-capitalize'>" . $temp . "</span>";
}, $string);
$ColorRegex = "<\[color\=([0-9A-Fa-f]){1,6}\](.*)?\[\/color\]>";
$string = preg_replace_callback($ColorRegex, function($matches) {
$match = $matches[0];
$temp = preg_replace_callback("<\[color\=([0-9A-Fa-f]){1,6}\]>", function($inner_matches) {
$element = explode("]", $inner_matches[0])[0];
$element = explode("[", $element)[1];
$element = explode("=", $element)[1];
return "<span style='color:#" . $element . ";'>";
}, $match);
$temp = preg_replace("<\[\/color\]>", "</span>", $temp);
return $temp;
}, $string);
$BackgroundRegex = "<\[background\=([0-9A-Fa-f]){1,6}\](.*)?\[\/background\]>";
$string = preg_replace_callback($BackgroundRegex, function($matches) {
$match = $matches[0];
$temp = preg_replace_callback("<\[background\=([0-9A-Fa-f]){1,6}\]>", function($inner_matches) {
$element = explode("]", $inner_matches[0])[0];
$element = explode("[", $element)[1];
$element = explode("=", $element)[1];
return "<span style='background-color:#" . $element . ";'>";
}, $match);
$temp = preg_replace("<\[\/background\]>", "</span>", $temp);
return $temp;
}, $string);
return $string;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment