2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

php stripslashes

Posted at

PHPを使ってDBに文字列を入れるとき

時々「"」や「'」を一緒に入れる時が訪れます。

$text = "php's sample";

みたいにあの文字列をただDBに入れると次のような結果がでます。

php\'s sample

「"」や「'」の前に「\」が勝手にくっ付くので取得して表示するのにややこしいことがあります。
その時に使うPHP関数としては「stripslashes」があります。

stripslashesは文字列に「\」がある場合、それを消してから値を戻してくれるありがたい関数です。

使い方は

echo stripslashes("php\'s sample");

になります。

結果は「php's sample」です。

PDOとかを使ったりすると

$stmt->bindValue(":db_text", stripslashes($postArray["text"]), PDO::PARAM_STR);

な感じで入れるだけで上の問題は解決できます。

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?