tic
tic

Reputation: 4189

putting a backslash before apostrophe

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

Upvotes: 1

Views: 7830

Answers (3)

The Blue Dog
The Blue Dog

Reputation: 2475

str_replace will do that for you.

str_replace("'", "\'", $mystring);

Upvotes: 1

Nick
Nick

Reputation: 4212

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);
?>

Upvotes: 7

Yehia Awad
Yehia Awad

Reputation: 2978

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

Upvotes: 3

Related Questions