1

Which function processes a string putting a backslash before every apostrophes? In other words, I want that

$string="L'isola";
echo somefunction($string);

returns:

L\'isola
tic
  • 4,009
  • 15
  • 45
  • 86
  • 3
    I think you're after `addslashes`. However, don't use this for inserting data into databases - use parameterisation instead. – halfer Dec 22 '13 at 20:27
  • This should get you what you want: [http://stackoverflow.com/questions/8251426/insert-string-at-specified-position][1] [1]: http://stackoverflow.com/questions/8251426/insert-string-at-specified-position – sinrise Dec 22 '13 at 20:28
  • OP stated that he wants a backslash before every apostrophe, not that he wants every special character escaping. – The Blue Dog Dec 22 '13 at 20:42

3 Answers3

7

You have to use addslashes() method to escape the quote Ex:

<?php
$str = "Is your name O'reilly?";

// Outputs: Is your name O\'reilly?
echo addslashes($str);
?> 

and the reverse method is stripslashes() which remove slashes Ex:

<?php
$str = "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>
Nick
  • 4,192
  • 1
  • 19
  • 30
2

you can use the addslashes() function more information can be found here

Yehia Awad
  • 2,898
  • 1
  • 20
  • 31
1

str_replace will do that for you.

str_replace("'", "\'", $mystring);
The Blue Dog
  • 2,475
  • 3
  • 19
  • 25