• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

PHP addslashes function – Quote string with slashes

by admin

Description

There are many situations where single quotes (‘), double quotes (“), and backslashes (\) can cause problems—databases, files, and some protocols require that you escape them with \, making \’, \”, and \\ respectively. In these circumstances, you should use the addslashes() function, which takes a string as its only parameter and returns the same string with these offending characters escaped so that they are safe for use. The stripslashes() function is the inverse for this function.

Syntax:

addslashes(string $string): string

Parameters

Parameter Description
string The string to be escaped.

Return Values

Returns the escaped string.

Examples

Example 1: A use case of addslashes() is escaping the aforementioned characters in a string that is to be evaluated by PHP:

<?php
$str = "Let's try this";
eval("echo '" . addslashes($str) . "';");
?>

Example 2

<?php
$hello=”Are you Jack’s brother?”; 
echo addslashes($hello);
?>

The output of the above program is:

Are you Jack\’s brother?
Note: If you can, use a database-specific escaping function instead of addslashes(). For example, if you’re using MySQL, use mysql_escape_string().

Filed Under: PHP

Some more articles you might also be interested in …

  1. PHP cos function – Cosine
  2. PHP ceil function – Round fractions up
  3. PHP function convert_cyr_string() – Convert from one Cyrillic character set to another
  4. “Access denied for user ‘username’@’hostname’ (using password: YES)” – Error while connecting MySQL with PHP
  5. PHP atanh function – Inverse hyperbolic tangent
  6. PHP bindec function – Convert a number between arbitrary bases
  7. PHP decbin function – Decimal to binary
  8. PHP function stripcslashes() – Un-quote string quoted with addcslashes()
  9. PHP atan2 function – Arc tangent of two variables
  10. PHP function chr() – Generate a single-byte string from a number

You May Also Like

Primary Sidebar

Recent Posts

  • protonvpn-cli Command Examples in Linux
  • protonvpn-cli connect Command Examples
  • procs Command Examples in Linux
  • prlimit: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright