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;
}
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;
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.
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);
This function is part of the WMPL-Plugin (Language-Selector) for Wordpress. What I need is a extension (if / else) of this function which makes it possible to show another flag-image on active sites.
function language_selector_flags(){
$languages = icl_get_languages('skip_missing=0&orderby=code');
if(!empty($languages)){
foreach($languages as $l){
if(!$l['active']) echo '<a href="'.$l['url'].'">';
echo '<img src="'.$l['country_flag_url'].'" height="12"
alt="'.$l['language_code'].'" width="18" />';
if(!$l['active']) echo '</a>';
}
}
}
The CSS isn't a problem, but my php-skills are on a very low level. It would be great if anyone can tell me a solution. Thanks for your helping me!
I believe this should do the trick:
function language_selector_flags(){
$languages = icl_get_languages('skip_missing=0&orderby=code');
if(!empty($languages)){
foreach($languages as $l){
if(!$l['active']) echo '<a href="'.$l['url'].'">';
if($l['active']){
echo '<img src="'.ACTIVE_FLAG_URL.'" height="12" alt="'.$l['language_code'].'" width="18" />';
}else{
echo '<img src="'.$l['country_flag_url'].'" height="12" alt="'.$l['language_code'].'" width="18" />';
}
if(!$l['active']) echo '</a>';
}
}
}
You need to substitute "ACTIVE_FLAG_URL" with your own URL.