Jump to content

Using a REPLACE only once


spacepoet

Recommended Posts

Hello:

 

I have a quick question.

 

I am currently using the following code to replace an apostrophe and quote marks when inserting and updating a database record:

$myPageContent = mysql_real_escape_string(str_replace("'", "'", $_POST['myPageContent']));
$myPageContent = mysql_real_escape_string(str_replace("", """, $_POST['myPageContent']));

 

My question is - how can I modify it so I only need to use one line.

Like (but this does not work):

$myPageContent = mysql_real_escape_string(str_replace("'", "'", "", """, $_POST['myPageContent']));

 

It seems to me that the way I am currently doing it might be POSTing the data twice?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/258935-using-a-replace-only-once/
Share on other sites

The first replace will essentially be ignored in your first code, because you aren't actually modifying the $_POST value.

 

You can use arrays in str_replace to replace multiple values.

$myPageContent = mysql_real_escape_string(str_replace(array('\'', '"'), array(''', '"'), $_POST['myPageContent']));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.