nl2br not working with BBCode - php
I had data in my table like this:
#inlcude<stdio.h>
main() {
printf("Hi");
}
But when I retrieve data from the database, it displays:
#inlude<stdio.h>main() { printf("Hi"); }
As you can see, there are no line breaks. Here is my entire code:
function BBCode ($str) {
$str = htmlentities($str);
$simple_search = array(
//added line break
'/\[br\]/is',
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[align\=(left|center|right)\](.*?)\[\/align\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
'/\[mail\](.*?)\[\/mail\]/is',
'/\[font\=(.*?)\](.*?)\[\/font\]/is',
'/\[size\=(.*?)\](.*?)\[\/size\]/is',
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
//added textarea for code presentation
'/\[codearea\](.*?)\[\/codearea\]/is',
//added paragraph
'/\[p\](.*?)\[\/p\]/is',
//smilies
'/\:angry:/is',
'/\:angel:/is',
'/\:arrow:/is',
'/\:at:/is',
'/\:biggrin:/is',
'/\:blank:/is',
'/\:blush:/is',
'/\:confused:/is',
'/\:cool:/is',
'/\:dodgy:/is',
'/\:exclamation:/is',
'/\:heart:/is',
'/\:huh:/is',
'/\:lightbulb:/is',
'/\:my:/is',
'/\:rolleyes:/is',
'/\:sad:/is',
'/\:shy:/is',
'/\:sleepy:/is',
'/\:smile:/is',
'/\:tongue:/is',
'/\:undecided:/is',
'/\:wink:/is',
// For 3rd Party Code
'/\[code\](.*?)\[\/code\]/is',
'/\[php\](.*?)\[\/php\]/is',
'/\[css\](.*?)\[\/css\]/is',
'/\[jscript\](.*?)\[\/jscript\]/is',
'/\[sql\](.*?)\[\/sql\]/is',
'/\[perl\](.*?)\[\/perl\]/is',
);
$simple_replace = array(
//added line break
'<br />',
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
// added nofollow to prevent spam
'$2',
'$1',
'<div style="text-align: $1;">$2</div>',
//added alt attribute for validation
'<img src="$1" alt="" />',
'$2',
'$1',
'<span style="font-family: $1;">$2</span>',
'<span style="font-size: $1;">$2</span>',
'<span style="color: $1;">$2</span>',
//added textarea for code presentation
'<textarea class="code_container" rows="20" cols="65">$1</textarea>',
//added paragraph
'<p>$1</p>',
//smilies
'<img src="comments/smilies/angry.gif" border="0" alt="" />',
'<img src="comments/smilies/angel.gif" border="0" alt="" />',
'<img src="comments/smilies/arrow.gif" border="0" alt="" />',
'<img src="comments/smilies/at.gif" border="0" alt="" />',
'<img src="comments/smilies/biggrin.gif" border="0" alt="" />',
'<img src="comments/smilies/blank.gif" border="0" alt="" />',
'<img src="comments/smilies/blush.gif" border="0" alt="" />',
'<img src="comments/smilies/confused.gif" border="0" alt="" />',
'<img src="comments/smilies/cool.gif" border="0" alt="" />',
'<img src="comments/smilies/dodgy.gif" border="0" alt="" />',
'<img src="comments/smilies/exclamation.gif" border="0" alt="" />',
'<img src="comments/smilies/heart.gif" border="0" alt="" />',
'<img src="comments/smilies/huh.gif" border="0" alt="" />',
'<img src="comments/smilies/lightbulb.gif" border="0" alt="" />',
'<img src="comments/smilies/my.gif" border="0" alt="" />',
'<img src="comments/smilies/rolleyes.gif" border="0" alt="" />',
'<img src="comments/smilies/sad.gif" border="0" alt="" />',
'<img src="comments/smilies/shy.gif" border="0" alt="" />',
'<img src="comments/smilies/sleepy.gif" border="0" alt="" />',
'<img src="comments/smilies/smile.gif" border="0" alt="" />',
'<img src="comments/smilies/tongue.gif" border="0" alt="" />',
'<img src="comments/smilies/undecided.gif" border="0" alt="" />',
'<img src="comments/smilies/wink.gif" border="0" alt="" />',
// For 3rd Party Code
'<pre class="brush: plain;">$1</pre>',
'<pre class="brush: php;">$1</pre>',
'<pre class="brush: css;">$1</pre>',
'<pre class="brush: jscript;">$1</pre>',
'<pre class="brush: sql;">$1</pre>',
'<pre class="brush: perl;">$1</pre>',
);
// Do simple BBCode's `
$str = preg_replace ($simple_search, $simple_replace, $str);
return $str;
}
function getComments($tutid){
//fetch all comments from database where the tutorial number is the one you are asking for:
$results = mysql_query("SELECT * FROM comments WHERE qazi_id='$tutid' ORDER BY id DESC") or die(mysql_error());
//find the number of comments:
$commentNum = mysql_num_rows($results);
echo' <div class="post-bottom-section">
<h2> '.$commentNum.' comments so far (post your own)</h2>
<div class="primary">
<ol class="commentlist">';
$count = mysql_numrows($results);
$i = 0;
while ($i < $count){
$qazi_id = mysql_result($results,$i,"qazi_id");
$name = mysql_result($results,$i,"name");
$email = mysql_result($results,$i,"email");
$description =mysql_result($results,$i,"description");
$url = mysql_result($results,$i,"url");
$date = mysql_result($results,$i,"date");
echo' <li class="'.$css.'">
<div class="comment-info">
<img alt="" src="'.$grav_url.' class="avatar" height="42" width="42" />
<cite>
'.$name.' Says: <br />
<span class="comment-data">'.$date.'</span>
</cite>
</div>
<div class="comment-text">
<p>'.BBCode($description).'</p>
</div>
</li>';
$i++;
}
echo'</ol></div></div>';
}`
If I replace $description =mysql_result($results,$i,"description"); with $description =nl2br(mysql_result($results,$i,"description")); the code displays output as
#include<stdio.h><br /> main() <br /> { <br /%gt; printf("hi");<br /> }
<br /> tags appears.
Jest replace return $str;
with
return nl2br($str);
Related
Detect emoji in plain text using PHP
I want to change expressions in the form of: [E-01] - [E-02] - [E-03] - [E-04] ... to <img src = "E-XX.png"> How can I do that? Input $message = "Hello [E-02] World [E-01]"; Desired Output Hello <img src="E-02.png"> World <img src="E-01.png">
Preg_replace is your friend: $message = "Hello [E-02] World [E-01]"; $replaced = preg_replace('/\[(E-\d\d)\]/', '<img src="/assets/images/emoji/$1.png" height="20" width="20" />', $message); echo $replaced; Output: Hello <img src="/assets/images/emoji/E-02.png" height="20" width="20" /> World <img src="/assets/images/emoji/E-01.png" height="20" width="20" />
function emoji($message){ for($e=1; $e <= 60; $e++){ $e = (($e < 10) ? '0' . $e : $e); $name = 'E-' . $e; $message = str_replace('[' . $name . ']', ' <img src="/assets/images/emoji/' . $name. '.png" height="20" width="20" /> ', $message); } return $message; }
Array only 1st emoticon replaced of my code
In my emoticon code, It replaced only 1st one of my code that mean only :) can display image. What is my wrong here in my code please. My code function smileys($text){ // Smiley to image $icons = array( ':)' => '<img src="smilies/smile.gif" border="0" alt="" />', ':-)' => '<img src="smilies/smile.gif" border="0" alt="" />', ':D' => '<img src="smilies/smile.gif" border="0" alt="" />', ':-(' => '<img src="smilies/angry.gif" border="0" alt="" />', 'angel' => '<img src="smilies/angel.gif" border="0" alt="" />', 'at' => '<img src="smilies/at.gif" border="0" alt="" />', ':-D' => '<img src="smilies/biggrin.gif" border="0" alt="" />', 'lol' => '<img src="smilies/biggrin.gif" border="0" alt="" />', ':-]' => '<img src="smilies/blush.gif" border="0" alt="" />', ':-?' => '<img src="smilies/confused.gif" border="0" alt="" />', 'B-)' => '<img src="smilies/cool.gif" border="0" alt="" />', 'B)' => '<img src="smilies/cool.gif" border="0" alt="" />', ';)' => '<img src="smilies/dodgy.gif" border="0" alt="" />', ':(' => '<img src="smilies/sad.gif" border="0" alt="" />', ':-(' => '<img src="smilies/sad.gif" border="0" alt="" />', 'shy' => '<img src="smilies/shy.gif" border="0" alt="" />', '|-)' => '<img src="smilies/sleepy.gif" border="0" alt="" />', ':-P' => '<img src="smilies/tongue.gif" border="0" alt="" />', ':-|' => '<img src="smilies/undecided.gif" border="0" alt="" />', ';-)' => '<img src="smilies/wink.gif" border="0" alt="" />', ); // Now you need find and replace foreach($icons as $search => $replace){ $text = preg_replace("#(?<=\s|^)" . preg_quote($search) . "#", $replace, $text); return $text; } } echo''.smileys($description).'';
This is because you are return in the loop after the first replace. Move the return outside of the loop. foreach ($icons as $search => $replace) { $text = preg_replace("#(?<=\s|^)" . preg_quote($search) . "#", $replace, $text); } return $text;
Cannot replace a same emoticon once more in a post
If I write a emoticon once more like " :) :) :) " its will replace only 1st one. Where is my problem here please. My php function smileys($text){ // Smiley to image $smileys = array( ':)' => '<img src="smilies/smile.gif" border="0" alt="" />', ':-)' => '<img src="smilies/smile.gif" border="0" alt="" />', ':D' => '<img src="smilies/smile.gif" border="0" alt="" />', ':-(' => '<img src="smilies/angry.gif" border="0" alt="" />', 'angel' => '<img src="smilies/angel.gif" border="0" alt="" />', ); // Now you need find and replace foreach ($smileys as $search => $replace) { $text = preg_replace("#(?<=\s|^)" . preg_quote($search) . "#", $replace, $text); } return $text; } echo''.smileys($description).'';
str_replace is the way to go. function smileys($text) { $toReplace = array( ':)', ':-)', ':D', ':-(', 'angel'); $replacement = array( '<img src="smilies/smile.gif" border="0" alt="" />', '<img src="smilies/smile.gif" border="0" alt="" />', '<img src="smilies/smile.gif" border="0" alt="" />', '<img src="smilies/angry.gif" border="0" alt="" />', '<img src="smilies/angel.gif" border="0" alt="" />'); return str_replace($toReplace,$replacement,$text); } This way you can replace all of the typed emotes to image tags, even different ones at once.
PHP preg_replace strings to images
When a user wants to send a message, he can use emoticons. What happens, is that a users clicks on an emoticon, so that it inserts the corresponding text, like this: :D Now, after the message has been sent, the other person wants to see it. What I want is to replace the :D with an image... Here is what I got: $patterns = array(); $patterns[0] = '/:)/'; $patterns[1] = '/:(/'; $patterns[2] = '/:D/'; $patterns[3] = '/:C/'; $patterns[4] = '/:A/'; $patterns[5] = '/:H/'; $patterns[6] = '/:L/'; $patterns[7] = '/:O/'; $patterns[8] = '/:S/'; $patterns[9] = '/;)/'; $replacements = array(); $replacements[0] = '<img alt=":)" border="0" src="./images/smileys/happy.png" width="25px" />'; $replacements[1] = '<img alt=":(" border="0" src="./images/smileys/sad.png" width="25px" />'; $replacements[2] = '<img alt=":D" border="0" src="./images/smileys/veryhappy.png" width="25px" />'; $replacements[3] = '<img alt=":C" border="0" src="./images/smileys/cry.png" width="25px" />'; $replacements[4] = '<img alt=":A" border="0" src="./images/smileys/angry.png" width="25px" />'; $replacements[5] = '<img alt=":H" border="0" src="./images/smileys/heart.png" width="25px" />'; $replacements[6] = '<img alt=":L" border="0" src="./images/smileys/love.png" width="25px" />'; $replacements[7] = '<img alt=":O" border="0" src="./images/smileys/nothing.png" width="25px" />'; $replacements[8] = '<img alt=":S" border="0" src="./images/smileys/scared.png" width="25px" />'; $replacements[9] = '<img alt=";)" border="0" src="./images/smileys/wink.png" width="25px" />'; preg_replace($patterns, $replacements, $bericht); But these aren't the right Regexes...so I get a php error. In the near future I'd like to take a tutorial on how regexes work, but I hope someone can help me out now. Is there a simple and quick way to do this? Preferably per pattern like I do now, not in 1 big regex.
Since you don't need regex search patterns you can simply use str_replace. From manual page: If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of preg_replace(). $bericht = str_replace($patterns, $replacements, $bericht);
Just escape the parenthesis: $patterns[0] = '/:\)/'; $patterns[1] = '/:\(/'; $patterns[9] = '/;\)/'; and also, change the last line to: $bericht = preg_replace($patterns, $replacements, $bericht);
You're replacing fixed strings, so there's absolutely no need to use regex patterns here at all; a straightforward str_replace() will do just fine. Or better yet, use strtr(), because then you can use an associative array for your replacement data... $replacements = array( ':)' = '<img alt=":)" border="0" src="./images/smileys/happy.png" width="25px" />', ':(' = '<img alt=":(" border="0" src="./images/smileys/sad.png" width="25px" />', ':D' = '<img alt=":D" border="0" src="./images/smileys/veryhappy.png" width="25px" />', ':C' = '<img alt=":C" border="0" src="./images/smileys/cry.png" width="25px" />', ':A' = '<img alt=":A" border="0" src="./images/smileys/angry.png" width="25px" />', ':H' = '<img alt=":H" border="0" src="./images/smileys/heart.png" width="25px" />', ':L' = '<img alt=":L" border="0" src="./images/smileys/love.png" width="25px" />', ':O' = '<img alt=":O" border="0" src="./images/smileys/nothing.png" width="25px" />', ':S' = '<img alt=":S" border="0" src="./images/smileys/scared.png" width="25px" />', ';)' = '<img alt=";)" border="0" src="./images/smileys/wink.png" width="25px" />', ); $output = strtr($input, $replacements); Much simpler! One thing you should watch out for is inadvertently replacing strings that aren't intended to be emoticons. This is quite a common problem for programs that do this kind of thing. (I've been caught out by Skype plenty of times converting things like (c) and 8) into icons when I didn't intend it to)
displaying a post thumbnail depending on category in Wordpress?
Is it possible to display a specific post thumbnail image based on the category ID, something like this: <?php if ( has_post_thumbnail() ) { if ( cat = 2 ) { echo '<img src="image1.jpg" width="" height="" class="live-holder-img" />'; } elseif( cat = 3 ) { echo '<img src="image2.jpg" width="" height="" class="live-holder-img" />'; } else { echo '<img src="default.jpg" width="" height="" class="default" />' } ?>
You may want to look into category templates: http://codex.wordpress.org/Category_Templates The quick solution would be something like this: if (is_category('1')) { echo '<img src="image1.jpg" width="" height="" class="live-holder-img" />'; } else if (is_category('2')) { echo '<img src="image2.jpg" width="" height="" class="live-holder-img" />'; } else { echo '<img src="default.jpg" width="" height="" class="default" />'; } //you can also do this by name if (is_category('Category A')) { echo '<img src="image1.jpg" width="" height="" class="live-holder-img" />'; } else if (is_category('Category B')) { echo '<img src="image2.jpg" width="" height="" class="live-holder-img" />'; } else { echo '<img src="default.jpg" width="" height="" class="default" />'; } is_category function reference: http://codex.wordpress.org/Function_Reference/is_category