String_replace replaces not all elements - php

I am making a Laravel Helper function, that should convert ":)" and others to normal smiles, however, some elements just do not convert! Maybe you can help me to find that, maybe stupid, mistake.
Code:
public static function convertSmiles($string)
{
$smiles = [
':)',
':angel:',
':angry:',
'8-)',
':\'(',
':ermm:',
':D',
'<3',
':(',
':O',
':P',
';)',
':alien:',
':blink:',
':blush:',
':cheerful:',
':devil:',
':dizzy:',
':getlost:',
':happy:',
':kissing:',
':ninja:',
':pinch:',
':pouty:',
':sick:',
':sideways:',
':silly:',
':sleeping:',
':unsure:',
':woot:',
':wassat:'
];
$urls = [
"<img src='".url('/jquery/sceditor/emoticons')."/smile.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/angel.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/angry.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/cool.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/cwy.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/ermm.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/grin.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/heart.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sad.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/shocked.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/tongue.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/wink.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/alien.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/blink.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/blush.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/cheerful.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/devil.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/dizzy.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/getlost.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/happy.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/kissing.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/ninja.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/pinch.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/pouty.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sick.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sideways.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/silly.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sleeping.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/unsure.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/w00t.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/wassat.png'/>"
];
return str_replace($smiles,$urls,$string);
}
I have problems with <3 and :'(

Related

JSON output images

Fair warning, I'm new to JSON and trying to get the output of a JSON file to properly display in PHP.
The JSON file I have contains:
{
"C1": [
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">"
],
"C10": [
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">"
],
"C11": [
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">"
],
"C12": [
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">"
]
}
This file has a lot more entries of C1, C10, W1, W20, W13 etc and they are generated randomly.
I'm able to get the output of the file in php with:
$getJSON = file_get_contents('file.json');
$json = json_decode($getJSON, true);
echo '<pre>' . print_r($json, true) . '</pre>';
But I want the output to be in the html form of:
C1 <img src=\"available.png\"> <img src=\"available.png\"> <img src=\"available.png\"> <img src=\"available.png\">
For each entry
Hopefully this makes sense
Try this:
$json = json_decode($getJSON, true);
foreach($json as $key=>$val){
echo $key." ".implode($val)."<br/>";
}
You can see it here
Note: if you want a space between the <img> tags, use implode(' ', $val)

preg_replace php image messup

Im trying to get the bbcode image to work. But i have problems with it.
what in the code is wrong and why?
I have this input:
[img]http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg[/img]
and would like it to be:
<img src="http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg">
but the result is:
`Q: How can i make this work properly?
Full code:
function parsebb($body,$userid = false, $user2 = false) {
$find = array(
"#\n#",
"#[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]#is",
"/\[url\=(.+?)\](.+?)\[\/url\]/is",
"/\[b\](.+?)\[\/b\]/is",
"/\[i\](.+?)\[\/i\]/is",
"/\[u\](.+?)\[\/u\]/is",
"/\[farge\=(.+?)\](.+?)\[\/farge\]/is",
"/\[size\=(.+?)\](.+?)\[\/size\]/is",
"/\[font\=(.+?)\](.+?)\[\/font\]/is",
"/\[boks\=venstre\](.+?)\[\/boks\]/is",
"/\[boks\=hoyre\](.+?)\[\/boks\]/is",
"/\[boks\=midten\](.+?)\[\/boks\]/is",
"/\[img\](.+?)\[\/img\]/is",
"/\[email\](.+?)\[\/email\]/is",
"/\[midten\](.+?)\[\/midten\]/is",
"/\[venstre\](.+?)\[\/venstre\]/is",
"/\[hoyre\](.+?)\[\/hoyre\]/is",
);
$replace = array(
"<br />",
"\\0",
"$2",
"<strong>$1</strong>",
"<em>$1</em>",
"<span style=\"text-decoration:underline;\">$1</span>",
"<font color=\"$1\">$2</font>",
"<font size=\"$1\">$2</font>",
"<span style=\"font-family: $1\">$2</span>",
"<div align=\"left\" style=\"text-align:center; width:50%; \">$1</div>",
"<div align=\"right\" style=\"text-align:center; width:50%;\">$1</div>",
"<div align=\"center\" style=\"text-align:center; width:50%;\">$1</div>",
"<img src=\"$1\">",
"$1",
"<div style=\"text-align:center;\">$1</div>",
"<div style=\"text-align:left;\">$1</div>",
"<div style=\"text-align:right;\">$1</div>",
);
$body = htmlspecialchars($body);
$body = preg_replace($specialCodes, $specialCodesReplace, $body);
return $body;
}
The problem is at your
"#[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]#is"
pattern, even [img] ... [/img] is matching with it just, comment them and try again, it will be repleaced in well form or put this pattern line to the end of the array

Return Unique Value from Chunked Array

I have a calendar script that outputs to a text file. I'm opening the text file, reading it into an array and then outputting the results. The text file contains:
7/9/2013-7/13/2013
Hot Stuff
By Robert More. Yes, folks, it's all platform shoes, leisure suits..
hotstuff.jpg
1,1,0,
*-*
7/16/2013-7/20/2013
Hot Stuff
By Robert More. Yes, folks, it's all platform shoes, leisure suits..
hotstuff.jpg
1,1,0,
*-*
My PHP code looks like this:
$content = file('DC_PictureCalendar/admin/database/cal2data.txt');
$content_chunked = array_chunk($content, 6);
if (count($content_chunked > 0))
{
echo "<table>";
for ($i=0;$i<count($content_chunked);$i++)
{
echo "<tr>";
echo "<td valign='top'>";
echo "<div style='padding-top:6px;'>";
echo "<a href='schedule.php'>";
echo "<img src='DC_PictureCalendar/admin/database/images/".$content_chunked[$i][3]."' width='80' height='80' border='2'>";
echo "</a>";
echo "</div>";
echo "</td>";
echo "<td valign='top'>";
echo "<div style='padding-left:5px;'>";
echo "<table>";
echo "<tr>";
echo "<td>";
echo "<h2>";
echo "<a href='schedule.php'>";
echo $content_chunked[$i][1];
echo "</a>";
echo "</h2>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo $content_chunked[$i][2];
echo "<a class='green' href='schedule.php'>";
echo "Read more..";
echo "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
Problem is, if there is a duplicate entry in the $content_chunked[$i][1] (which is the title in this case), I just want to display it once instead of twice. Is this possible? I thought array_unique might work but it didn't seem to help. Thanks in advance!
array_unique() RETURNS the array without duplicates, but does NOT modify the original array!
so:
$a = [1, 1, 2, 3]
array_unique($a) => [1, 2, 3]
$a => [1, 1, 2, 3]
The new array needs to be saved in a variable for you to access it later.
$a = [1, 1, 2, 3]
$b = array_unique($a)
$b => [1, 2, 3]
Although its possibly not the most graceful / efficient.. Add this in in the relevant place:
echo "<table>";
$titles = array();
for ($i=0;$i<count($content_chunked);$i++)
{
if (in_array($content_chunked[$i][1], $titles)) continue;
$titles[] = $content_chunked[$i][1]

HTML img in PHP variable

Im trying to do this
$input = "<img src="HTML/images/user.png" alt="" />";
but it does not work out,
i know im supposed to put a / before a " or something please help
Try this
$input = "<img src='HTML/images/user.png' alt='' />";
Or
$input = "<img src=\"HTML/images/user.png\" alt=\"\" />";
all you need to do is escape double quote.
just like below.
\"HTML/images/user.png\" alt=\"\"
Note that you have use " for PHP already.
You can either choose to use ' or \".
For example,
$input = "<img src='HTML/images/user.png' alt='' />";
$input = "<img src=\"HTML/images/user.png\" alt=\"\" />";
Try this
$input = "<img src='HTML/images/user.png' alt='' />";

PHP doesn't execute function

SOLVED: Read the comments below #Eray.
I have a PHP function to look through text and convert text emoticons to images. :), :(, :|, etc. I also have a function that looks through text and replaces BBCode with HTML. I execute these on a string from a database. Both of these use the global variable $newtext.
emoticon($row['words']);
bb($row['words']);
echo "<b>" . $row['username'] . "</b> - " . $row['time'];
echo "<p>" . $newtext . "</p>";
echo "";
The odd thing about this, is that now (I can't remember what I did) the emoticon function doesn't work, but the bb function does. By doesn't work, I mean doesn't replace anything. Text remains text. This had worked before. Also, every few times, $newtext comes before the username. Here are my functions...
function emoticon($text)
{
global $newtext;
$newtext=str_replace(":)", "<img src='emoticons/smile.gif'>", $text);
$newtext=str_replace(":(", "<img src='emoticons/sad.gif'>", $newtext);
$newtext=str_replace(":D", "<img src='emoticons/biggrin.gif'>", $newtext);
$newtext=str_replace(":p", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":P", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":|", "<img src='emoticons/neutral.gif'>", $newtext);
$newtext=str_replace("8)", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace("8D", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace(":o", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(":O", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(";)", "<img src='emoticons/wink.gif'>", $newtext);
$newtext=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $newtext);
}
function bb($text)
{
global $newtext;
$array=array(
"[b]" => "<b>",
"[/b]" => "</b>",
"[i]" => "<i>",
"[/i]" => "</i>",
"[u]" => "<u>",
"[/u]" => "</u>",
"[big]" => "<h1>",
"[/big]" => "</h1>",
);
$newtext = str_ireplace(array_keys($array), array_values($array), $text);
}
Could you explain or help me? Also, is there a better way than using global variables? I know they can be a bit "dangerous."
function emoticon($text)
{
$smiley = array(
':)',
':(',
);
$replace = array(
"<img src='emoticons/smile.gif'>",
"<img src='emoticons/sad.gif'>",
);
return str_replace($smiley, $replace, $text);
}
I think you'd better do this:
$text = $row['words'];
$text = emoticon($text);
$text = bb($text);
echo "<b>" . $row['username'] . "</b> - " . $row['time'];
echo "<p>" . $text . "</p>";
echo "";
And then edit your functions like this:
function emoticon($text)
{
// remove this line: global $newtext;
$text=str_replace(":)", "<img src='emoticons/smile.gif'>", $text);
// etc...
$text=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $text);
return $text;
}
function bb($text)
{
// remove this line: global $newtext;
$array=array(
// etc...
);
return str_ireplace(array_keys($array), array_values($array), $text);
}
First, DEFINE your PHP functions:
function emoticon ( $string )
{
$emoticons = array( ':)' , ';)' );
$icons = ('happy.gif','wink.gif');
return str_replace( $emoticons, $icons , $string );
}
function bb( $string )
{
//BOLD [b]text[/b]
$string = preg_replace('/(\[b\]([\w\d\s\.]+)\[\/b\])/i','<b>$2</b>',$string);
//ITALIC [i]text[/i]
$string = preg_replace('/(\[i\]([\w\d\s\.]+)\[\/i\])/i','<em>$2</em>',$string);
//UNDERLINE [u]text[/u]
$string = preg_replace('/(\[u\]([\w\d\s\.]+)\[\/u\])/i','<u>$2</u>',$string);
return $string;
}
Second, Call you PHP defined functions:
echo bb( "[b]Bold[/b]" ); //Return <b>Bold</b>
Bold
echo bb( "[i]Italic[/i]" ); //Return <em>Italic</em>
Bold
echo bb( "[i]My [b]Text[/b][/i]" ); //Return <em>My <b>Text</b></em>
My Text
$var = $_POST['foo'];
echo bb( $var );
Third, Test your code:
emoticon($row['words']);
bb($row['words']);
echo "<b>" . $row['username'] . "</b> - " . $row['time'];
echo "<p>" . $newtext . "</p>";
echo "";
didnt test it but this should do:
notice that instead of using global $newtext i'm creating a local variable and return it to the outer scope via return.
function emoticon($text) {
$newtext=str_replace(":)", "<img src='emoticons/smile.gif'>", $text);
$newtext=str_replace(":(", "<img src='emoticons/sad.gif'>", $newtext);
$newtext=str_replace(":D", "<img src='emoticons/biggrin.gif'>", $newtext);
$newtext=str_replace(":p", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":P", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":|", "<img src='emoticons/neutral.gif'>", $newtext);
$newtext=str_replace("8)", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace("8D", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace(":o", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(":O", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(";)", "<img src='emoticons/wink.gif'>", $newtext);
$newtext=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $newtext);
return $newtext;
}
function bb($text) {
$array=array(
"[b]" => "<b>",
"[/b]" => "</b>",
"[i]" => "<i>",
"[/i]" => "</i>",
"[u]" => "<u>",
"[/u]" => "</u>",
"[big]" => "<h1>",
"[/big]" => "</h1>",
);
return str_ireplace(array_keys($array), array_values($array), $text);
}
$row['words'] = emoticon( $row['words'] );
$row['words'] = bb( $row['words'] );

Categories