Hey folks and folksesses,
I have written some code to get emoticons onto my image based shoutbox but there is quite a large bug, as there is with any code in the early stages, and I need your help to narrow it down for me.
The idea is that the script pulls data from the database - username, colour, font and shout to print '[username] shout' onto the image. This part of the code all works perfectly. Then it looks through each line at a time to see if there is either :D, :), :P or :( in the shout, if there is it finds the position of the string it's at, uses this data * a number to get the emote to the correct place on the image. After it has found that result and merged the emoticon onto the shoutbox, it deletes the :D, :), :P or :( from that shout, depending on which was used from the shout so that there is no overlapping of text and image.
This works fine for the :D - the test post "Yay!! :D" gives this result (top line)
Whereas the post "Yay!! :P" gives the result
^ it deletes the entire username and shout from the image and merges the :) emoticon into place.
showimage_a.php -
<?php
-
$name = "[" . htmlspecialchars_decode($row['username']) . "] "; //it works - returns username
-
$font = $row['font']; //it works - renders the colour
-
$color = $row['color']; //it works - changes the font
-
$line = htmlspecialchars_decode($row['comment']); //it works, posts the shout
-
-
-
if ($name == "[] ")
-
{
-
$name = "";
-
-
$line = $name . $line;
-
}
-
else
-
{
-
-
$line = $name . $line;
-
}
-
-
-
if (isset($_SESSION['sad'])) // not entirely implemented, does not register a session yet
-
{
-
-
$sad = $_SESSION['sad'];
-
$sad_pos = strpos($line, $sad);
-
-
if ($font == "palab") {
-
$postb = $sad_pos * 5.4;
-
} elseif ($font == "comicsans") {
-
$postb = $sad_pos * 6.725;
-
} else {
-
}
-
-
if (stristr($line, $sad))
-
{
-
-
imagecopymerge($image, $sad_im, ($cur_line_x + $postb), (($cur_line_y + $lineheight) - 30), 0,
-
0, 15, 15, 100);
-
-
$line = preg_replace('/:(/', "", $line);
-
}
-
}
-
-
if (isset($_SESSION['smiley']))
-
{
-
-
$smiley = $_SESSION['smiley'];
-
$smile_pos = strpos($line, ":)");
-
-
if ($font == "palab") {
-
$posts = $smile_pos * 5.36;
-
} elseif ($font == "comicsans") {
-
$posts = $smile_pos * 6.725;
-
} else {
-
}
-
-
if (stristr($line, $smiley))
-
{
-
-
imagecopymerge($image, $smile_im, ($cur_line_x + $posts), (($cur_line_y + $lineheight) - 30), 0,
-
0, 15, 15, 100);
-
-
$line = preg_replace(':)', "", $line); //doesn't render on some occassions due to sessions - not causing this problem.
-
}
-
}
-
-
-
if (isset($_SESSION['tongue']))
-
{
-
-
$tongue = $_SESSION['tongue'];
-
$ton_pos = strpos($line, $tongue);
-
-
if ($font == "palab") {
-
$postt = $ton_pos * 5.1;
-
} elseif ($font == "comicsans") {
-
$postt = $ton_pos * 6.725;
-
} else {
-
}
-
-
if (stristr($line, $tongue))
-
{
-
-
imagecopymerge($image, $tongue_im, ($cur_line_x + $postt), (($cur_line_y + $lineheight) - 30), 0,
-
0, 15, 15, 100);
-
-
$line = preg_replace('/:P/', "", $line); //For some reason removes the entire $line and adds $smile_im to the image. *confused*
-
}
-
}
-
-
-
if (isset($_SESSION['grin'])) {
-
-
$grin = $_SESSION['grin'];
-
$pos = strpos($line, $grin);
-
-
if ($font == "palab") {
-
$post = $pos * 5.7;
-
} elseif ($font == "comicsans") {
-
$post = $pos * 6.725;
-
} else {
-
}
-
-
if (stristr($line, $grin)) {
-
-
-
imagecopymerge($image, $grin_im, ($cur_line_x + $post), (($cur_line_y + $lineheight)- 30.5), 0, 0,
-
15, 15, 100);
-
-
$line = preg_replace('/:D/', "", $line); // This bit works fine (gives the first result from Yay!! :D
-
-
}
-
-
}
-
-
-
if ($color == "white" || $color == "yellow" || $color == "green" || $color ==
-
"orange" || $color == "aqua")
-
{
-
Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, getColor($color), getfont
-
($font), trim($line));
-
-
-
}
-
elseif ($color != "white" || $color != "yellow" || $color != "green" || $color !=
-
"orange" || $color != "aqua")
-
{
-
Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, $white, getfont($font),
-
trim($line));
-
-
-
}
-
elseif ($font == "fixedsys" || $font == "Courbd" || $font == "arial" || $font ==
-
"timesnr" || $font == "calibri" || $font == "comicsans" || $font == "palab")
-
{
-
Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, getColor($color), getfont
-
($font), trim($line));
-
-
-
}
-
else
-
{
-
Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, $white, "courbd.ttf", trim
-
($line));
-
-
-
}
-
?>
-
Hope that makes sense.
Any ideas?
Please reply,
Sam
code=php tags not working again. =\
5 2166
Fixed it!
Simply changed preg_replace to str_replace.
Always seems to be the simplest things..
Thanks anyway,
Sam
hsriat 1,654
Recognized Expert Top Contributor
Fixed it!
Simply changed preg_replace to str_replace.
Always seems to be the simplest things..
Thanks anyway,
Sam
Is that working if you have more then one same kind of smiley?
:D :D :D :D or :) :) :) :)
Is that working if you have more then one same kind of smiley?
:D :D :D :D or :) :) :) :)
Hmm.. you make a good point. No, it only allows one of each..
Any solutions?
Sam
pbmods 5,821
Recognized Expert Expert
Heya, Sam.
It looks like your script is trying to 'replace' the smiley, I think by calculating where the emoticon text would be and replacing it with the graphic.
I'm curious how it would handle a smiley in the middle of a line.
E.g., "Ooh, I'm gonna mess with >:) your system!"
Perhaps a better way would be to use preg_split() to divide up your string and then process it in chunks.
E.g.,: -
/** Note the fourth parameter. This means that the delimiters will also be included in $chunks! */
-
$chunks = preg_split('/(:(?:\\)|P|D))/', $line, -1, PREG_SPLIT_DELIM_CAPTURE);
-
-
/** If there's more than one element in $chunks, we have at least one smiley! */
-
if( isset($chunks[1]) )
-
{
-
$i = 0;
-
-
do
-
{
-
/** We know that even-indexed chunks are plain text, so we can automatically output those. */
-
output_text($chunks[$i]);
-
-
/** Odd-indexed chunks are smileys. */
-
if( isset($chunks[++$i]) )
-
{
-
output_smiley($chunks[$i]);
-
}
-
}
-
while( isset($chunks[++$i]);
-
}
-
Let's suppose $line was set to 'Smileys :) beautiful :P smileys :D'.
Here's what $chunks would look like: -
Array
-
(
-
[0] => Smileys
-
[1] => :)
-
[2] => beautiful
-
[3] => :P
-
[4] => smileys
-
[5] => :D
-
[6] =>
-
)
-
Note that all the even-indexed elements are plain text, and all the odd-indexed elements are smileys.
Incidentally, I thought one of those Usernames was interesting. I can't remember which one it was, exactly... my mind's gone a bit... fuzzy.....
Hey, pbmods.
Yeah, I'll give that a try; have a mess around with it. There are a few bugs to be ironed out of my initial code, but sometimes the emoticon will appear in the space between the text in "Ooh, I'm going to mess with :@ your system" or whatever it may be. However sometimes it appears over the 'th' in the word 'with', because it's the string position * 5.7 (trial and error, that works on the most part).
I shall give your way a go.
As for the username, I've only put word filtering on the message. Reckon I should filter the username? :P
Sam
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: TXSherry |
last post by:
Hi,
I cannot seem to wrap my brain around preg_replace. Though I've read
the help file backwords and forwards. :/ Hoping someone can give me
a solution here.
Problem: Given string 'str'...
|
by: yawnmoth |
last post by:
say i have the following script:
<?
$test = "aaaaa";
print '"' . preg_replace('/.*/','x',$test) . '"<br>';
$test = "\n\n\n\n\n";
print '"' . preg_replace('/.*/','x',$test) . '"';
?>
the...
|
by: Margaret MacDonald |
last post by:
I've been going mad trying to figure out how to do this--it should be
easy!
Allow the user to enter '\_sometext\_', i.e., literal backslash,
underscore, some text, literal backslash, underscore...
|
by: rybasso |
last post by:
Hello,
Does anyone has an idea why preg_replace() cause 'This page contains no
data' in FF and 'This page could not be found' in IE.
I spend a lot of time searching any ideas on newsgroups but...
|
by: Dave |
last post by:
Hi,
This is an interesting problem I'm faced with. I have been trying all sorts
of functions to fix it and my last resort is to ask you guys and girls.
I have an array:...
| |
by: monomaniac21 |
last post by:
hi all
using preg_replace
how can i replace the letter i in a string with nothing (delete it)
when it is the last letter or it is followed by an i?
i have products that are listed in a db...
|
by: Terence |
last post by:
I've been puzzling over something for ages and now give up:
Does anyone see any problem with any of the following:
$line = preg_replace('/\{1,6})\]/i', "<font color='#
${1}'>", $line);
...
|
by: K. |
last post by:
Hello!
I would like to use preg_replace function to leave in the result variable
only:
1) letters from A to Z
including Polish letters like ±¶¼³óñê
including Polish upper letters like ¡¦¬£ÓÑÊ...
|
by: Silvertype |
last post by:
Hi guys,
I have these html tags:
<input type="text" id="txtName" name="txtName" />
<textarea id="taHello" name="taHello">hello world</textarea>
which I want to replace with the following...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |