Connecting Tech Pros Worldwide Forums | Help | Site Map

How to Delete a character from a string

Familiar Sight
 
Join Date: Sep 2007
Posts: 181
#1: Jul 10 '08
Hi this is probably a very simple question but how do you remove characters from strings i have already tried ltrim and str_replace but the certain character still stayed in the string.

Not sure if its just the character im searching for or if its the function itself which is screwing up.

Any how the actual character is --> ' <-- in other words an Apostrophe and here is an example of the string im trying to remove it from
--> "Here's a fantastic opportunity".

The reason im trying to do this is because when i write to a database i get an error with this specific character so i thought just remove it.

Thanks for any help in advanced,

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#2: Jul 10 '08

re: How to Delete a character from a string


i don't see why str_replace wouldn't work..

But anyway, the problem you're encountering is not the problem you think it is, but a security risk. Read mysql_injection(). You can over come this by using mysql_real_escape_string().

Kind regards.
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#3: Jul 10 '08

re: How to Delete a character from a string


You need addslashes.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#4: Jul 10 '08

re: How to Delete a character from a string


Quote:

Originally Posted by hsriat

You need addslashes.

But when dealing with a DataBase, I think it's more ideal to use mysql_real...() because it escapes all special characters concerned with a DB.
Familiar Sight
 
Join Date: Sep 2007
Posts: 181
#5: Jul 10 '08

re: How to Delete a character from a string


Ok i understand that such characters can be used to determine wether a database is susceptable to injections, but this is not some much the reason im doing this its more the reason im trying to store data and the appostrephe is screwing this up.

So i looked at both the escape characters and the addslahes, the escape characters look more like what i need but only a slight problem i cant read german and don't understand the explanations of each of the parameters of the function.

So could you please post a reasonably example of this function in action.

Thanks for the help

[EDIT]

Lol i didn't realise that you change the page to english, anyway i understand the function now but can i use the function without a link indentifyier; Can this function be used on a straight string in php without anything to do with mysql??

Becuase i want to remove any specail characters before making any my_sql connections
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#6: Jul 10 '08

re: How to Delete a character from a string


Quote:

Originally Posted by chazzy69

Ok i understand that such characters can be used to determine wether a database is susceptable to injections, but this is not some much the reason im doing this its more the reason im trying to store data and the appostrephe is screwing this up.

So i looked at both the escape characters and the addslahes, the escape characters look more like what i need but only a slight problem i cant read german and don't understand the explanations of each of the parameters of the function.

So could you please post a reasonably example of this function in action.

Thanks for the help

[EDIT]

Lol i didn't realise that you change the page to english, anyway i understand the function now but can i use the function without a link indentifyier; Can this function be used on a straight string in php without anything to do with mysql??

Becuase i want to remove any specail characters before making any my_sql connections

There has to be a mysql connection present for this to work.

It'll make no difference if you do the escaping after the connection is established. You don't *need* a link identifier; the last connection will be assumed.
Familiar Sight
 
Join Date: Sep 2007
Posts: 181
#7: Jul 11 '08

re: How to Delete a character from a string


Ok i have solved my problem and i didn't actually needed the escape characters msql function due to the fact that it would have deleted some of the stuff im trying to save into my database.

But anyway the problem was, i was using the str_ireplace function incorrectly thanks for the help anyway. And i will try to remember that escape characters function for future reference.


Thanks for all your help
Reply