473,721 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preg_replace problem

118 New Member
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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.         $name = "[" . htmlspecialchars_decode($row['username']) . "] "; //it works - returns username
  3.         $font = $row['font']; //it works - renders the colour
  4.         $color = $row['color']; //it works - changes the font
  5.         $line = htmlspecialchars_decode($row['comment']); //it works, posts the shout
  6.  
  7.  
  8.         if ($name == "[] ")
  9.         {
  10.             $name = "";
  11.  
  12.             $line = $name . $line;
  13.         }
  14.         else
  15.         {
  16.  
  17.             $line = $name . $line;
  18.         }
  19.  
  20.  
  21.  if (isset($_SESSION['sad'])) // not entirely implemented, does not register a session yet
  22.         {
  23.  
  24.             $sad = $_SESSION['sad'];
  25.             $sad_pos = strpos($line, $sad);
  26.  
  27.             if ($font == "palab") {
  28.         $postb = $sad_pos * 5.4;
  29.         } elseif ($font == "comicsans") {
  30.         $postb = $sad_pos * 6.725;
  31.         } else {
  32.         }
  33.  
  34.             if (stristr($line, $sad))
  35.             {
  36.  
  37.                 imagecopymerge($image, $sad_im, ($cur_line_x + $postb), (($cur_line_y + $lineheight)  - 30), 0,
  38.                     0, 15, 15, 100);
  39.  
  40.               $line = preg_replace('/:(/', "", $line); 
  41.             }
  42.         }
  43.  
  44.  if (isset($_SESSION['smiley']))
  45.         {
  46.  
  47.             $smiley = $_SESSION['smiley'];
  48.             $smile_pos = strpos($line, ":)");
  49.  
  50.             if ($font == "palab") {
  51.         $posts = $smile_pos * 5.36;
  52.         } elseif ($font == "comicsans") {
  53.         $posts = $smile_pos * 6.725;
  54.         } else {
  55.         }
  56.  
  57.             if (stristr($line, $smiley))
  58.             {
  59.  
  60.                 imagecopymerge($image, $smile_im, ($cur_line_x + $posts), (($cur_line_y + $lineheight) - 30), 0,
  61.                     0, 15, 15, 100);
  62.  
  63.            $line =  preg_replace(':)', "", $line); //doesn't render on some occassions due to sessions - not causing this problem.
  64.             }
  65.         }
  66.  
  67.  
  68.  if (isset($_SESSION['tongue']))
  69.         {
  70.  
  71.             $tongue = $_SESSION['tongue'];
  72.             $ton_pos = strpos($line, $tongue);
  73.  
  74.             if ($font == "palab") {
  75.         $postt = $ton_pos * 5.1;
  76.         } elseif ($font == "comicsans") {
  77.         $postt = $ton_pos * 6.725;
  78.         } else {
  79.         }
  80.  
  81.             if (stristr($line, $tongue))
  82.             {
  83.  
  84.                 imagecopymerge($image, $tongue_im, ($cur_line_x + $postt), (($cur_line_y + $lineheight) - 30), 0,
  85.                     0, 15, 15, 100);
  86.  
  87.           $line =  preg_replace('/:P/', "", $line); //For some reason removes the entire $line and adds $smile_im to the image. *confused*
  88.             }
  89.         }
  90.  
  91.  
  92.         if (isset($_SESSION['grin'])) {
  93.  
  94.         $grin = $_SESSION['grin'];
  95.         $pos = strpos($line, $grin);
  96.  
  97.       if ($font == "palab") {
  98.         $post = $pos * 5.7;
  99.         } elseif ($font == "comicsans") {
  100.         $post = $pos * 6.725;
  101.         } else {
  102.         }
  103.  
  104.         if (stristr($line, $grin)) {
  105.  
  106.  
  107.         imagecopymerge($image, $grin_im, ($cur_line_x + $post), (($cur_line_y + $lineheight)- 30.5), 0, 0,
  108.         15, 15, 100);
  109.  
  110.       $line = preg_replace('/:D/', "", $line); // This bit works fine (gives the first result from Yay!! :D
  111.  
  112.         }
  113.  
  114.         } 
  115.  
  116.  
  117.         if ($color == "white" || $color == "yellow" || $color == "green" || $color ==
  118.             "orange" || $color == "aqua")
  119.         {
  120.             Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, getColor($color), getfont
  121.                 ($font), trim($line));
  122.  
  123.  
  124.         }
  125.         elseif ($color != "white" || $color != "yellow" || $color != "green" || $color !=
  126.             "orange" || $color != "aqua")
  127.         {
  128.             Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, $white, getfont($font),
  129.                 trim($line));
  130.  
  131.  
  132.         }
  133.         elseif ($font == "fixedsys" || $font == "Courbd" || $font == "arial" || $font ==
  134.             "timesnr" || $font == "calibri" || $font == "comicsans" || $font == "palab")
  135.         {
  136.             Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, getColor($color), getfont
  137.                 ($font), trim($line));
  138.  
  139.  
  140.         }
  141.         else
  142.         {
  143.             Imagettftext($image, 10, 0, $cur_line_x, $cur_line_y, $white, "courbd.ttf", trim
  144.                 ($line));
  145.  
  146.  
  147.         }
  148. ?>
  149.  
Hope that makes sense.
Any ideas?

Please reply,
Sam

code=php tags not working again. =\
May 31 '08 #1
5 2178
helraizer1
118 New Member
Fixed it!

Simply changed preg_replace to str_replace.

Always seems to be the simplest things..

Thanks anyway,
Sam
May 31 '08 #2
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 :) :) :) :)
May 31 '08 #3
helraizer1
118 New Member
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
Jun 1 '08 #4
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.,:
Expand|Select|Wrap|Line Numbers
  1. /** Note the fourth parameter.  This means that the delimiters will also be included in $chunks! */
  2. $chunks = preg_split('/(:(?:\\)|P|D))/', $line, -1, PREG_SPLIT_DELIM_CAPTURE);
  3.  
  4. /** If there's more than one element in $chunks, we have at least one smiley! */
  5. if( isset($chunks[1]) )
  6. {
  7.   $i = 0;
  8.  
  9.   do
  10.   {
  11.     /** We know that even-indexed chunks are plain text, so we can automatically output those. */
  12.     output_text($chunks[$i]);
  13.  
  14.     /** Odd-indexed chunks are smileys. */
  15.     if( isset($chunks[++$i]) )
  16.     {
  17.         output_smiley($chunks[$i]);
  18.     }
  19.   }
  20.   while( isset($chunks[++$i]);
  21. }
  22.  
Let's suppose $line was set to 'Smileys :) beautiful :P smileys :D'.

Here's what $chunks would look like:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [0] => Smileys 
  4.     [1] => :)
  5.     [2] =>  beautiful 
  6.     [3] => :P
  7.     [4] =>  smileys 
  8.     [5] => :D
  9.     [6] => 
  10. )
  11.  
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.....
Jun 1 '08 #5
helraizer1
118 New Member
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
Jun 1 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4320
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' which may contain new lines and will contain html code, IN this string any "words" that begin with an underscore I want to replace with a given word. A word here being a group of chars preceded by a space or null (start of line) and closed by a...
1
2243
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 output i would expect is as follows:
7
4996
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 and, after submitting via POST to a preg_replace filter, get back '_sometext_' (i.e., the same thing with the literal backslashes stripped)
1
1781
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 found nothing corresponding. Here is the code:
1
2508
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: $arr(=>"aaa",=>"bbb",=>"ccc",=>"etc"); $html='<html>....<a href="page.php?var=3">3</a><a href="page.php?var=345">345</a...<a href="page.php?var=n">n</a>...</html>';
7
2038
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 with i or ii as in 320ii and i want to strip out the i's at the end when displaying the product name
2
1536
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); $line = preg_replace('/\){3}((){3})?))\]/ i', "<font color='#${1}'>", $line);
1
1297
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 ¡¦¬£ÓÑÊ 2) letters ()-_=+!@#$%^&*`~',/\|. I have for example such varaible
3
1015
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 tags:
0
8840
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9064
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8007
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3189
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 we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.