Solved

PHP escape chars and ereg replace

Posted on 2004-03-30
5
1,206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 27

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 27

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 27

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code not executing correctly. 3 43
Php variable to be sent back 3 34
Undefined variable with $_POST in PHP 5 39
Conditional Array Element 7 28
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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