Solved

PHP escape chars and ereg replace

Posted on 2004-03-30
5
1,196 Views
Last Modified: 2013-12-12
I have a little problem which has been driving me insane for about an hour.
I am developing a message board / forum much like this. It has smilies a admin user can add and edit smilies fine. The problem comes when posting users click on the smilie they desire and a little bit of text goes into the post box.
Later on the code below is ran and the text e.g :) is replaced with an image.
Well thats the plan.
but i get this nice error
Warning: ereg_replace(): REG_EMPTY:Hempty (sub)expression in C:\Webspace\s2ointernal\Development\cmsdev.s2o.co.uk\www\posts.php on line 144
 
O.k I think check the code make sure the brackets are all o.k.
check, they are.
I then relise that the ) that has been grabbed from the database in order to search for the smilie characters which will then be replaced is closing the ereg_replace early or at least thats that I think
I tried to replace it with no avail. Any ideas will be great.
                        
$query2="SELECT * FROM smilies";//table which holds the image and replace text
$result2=odbc_exec($handle,$query2);//run it
$postData=$_POST['postdata'];//the post data
$i=0;
while(odbc_fetch_row($result2))//go through each enty
{
$string=ereg_replace("/)","//)", odbc_result($result2,"replace"));//try to replace the ) in the replace text
      echo $string;//echo it out error here
$postData=ereg_replace($string,"<img src=" . ereg_replace(" ","%20",odbc_result($result2,"image")) . ">",$postData);//replace the image
                                                
}
0
Comment
Question by:rhousham2
  • 3
5 Comments
 
LVL 26

Accepted Solution

by:
skullnobrains earned 50 total points
ID: 10715232
dunno much about ereg_replace errors as i make little use of it but str_replace is much easier to handle and should solve your problem.
if what you replace the :) by is an image you just can't use any of them. in this case try to either use php to directly output the image to the browser or use the classical html tag (<img...)
if not, could u tell what u want it to be replaced by ??
0
 
LVL 6

Expert Comment

by:Reapz
ID: 10715746
Are you sure that the correct text is being returned form the DB? When putting text into a DB you should escape any reserved characters first. You can use addslashes() to this this and then when you pull the data out of the DB use stripslashes() to remove the escaping slashes.
0
 

Author Comment

by:rhousham2
ID: 10716063
O.k the skullnobrains guy is right your str_replace thing worked! . The point will be yours shortly cheers. To Reapz the addslashes() function only adds / before and ' or " so the ) will not have been escaped nor should it be, should it?. Not too sure there. Cheers for telling me about the addslashes and remove slashes though cos previously i've been using ereg_replace and then replaceing the " and ' with &nb34; or &nb39 or somethin like that.
but cheers to the first guy the points r yours
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 10719191
cool if u managed
if u run through any more probs, i guess u will post it down here.
if needed i just don't care thatmuch about points so you may post here.
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 10721478
sorry, was tired yesterday so i didn't really get this was a question.

<< so the ) will not have been escaped nor should it be, should it?. Not too sure there. Cheers for telling me about the addslashes and remove slashes though cos previously i've been using ereg_replace and then replaceing the " and ' with &nb34; or &nb39 or somethin like that. >>

addslashes (and the reverse function stripslashes) only affect the quotes, double-quotes, anti-slashes and null characters. it is expressly designed to escape strings for a SGBD input (ie requests on any BDD) and should be used for user text inputToBdd ie forums for example.

for more info, type addslashes in google, as almost any php function, the first choice will be one of the php manuals at the correct page. you definitely MUST have a copy of that manual if u don't usually work online. it is valuable, contains lots of hints and tricks as well as some usual errors and/or mistakes. briefly it is excellent in every way and php is the sole language in this case !

gluck;
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now