• 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 function stripslashes() – Un-quotes a quoted string

by admin

The stripslashes() function is used to unquote a string that is quoted by using the addslashes() function. This function was introduced in PHP3. The stripslashes() function is the opposite of addslashes(): it removes one set of \-escapes from a string.

Syntax:

stripslashes(string $string): string

To remove backslashes and convert C-style escape sequences to their literal values:

stripcslashes() 

To add slashes to a string:

addslashes() 

Parameters

Parameter Description
string The string to be unescaped.

Return Values

Returns a string with backslashes stripped off. (\’ becomes ‘ and so on.) Double backslashes (\\) are made into a single backslash (\).

Examples

Example 1:

<?php
$str = “Are you Jack?”; // Outputs: Are you Jack? echo stripslashes($str); ?>

Example 2:

$string = "I'm a lumberjack and I'm okay!";
$a = addslashes($string);
// string is now "I\'m a lumberjack and I\'m okay!"

$b = stripslashes($a);
// string is now "I'm a lumberjack and I'm okay!"

Example 3: Remove slashes from data retrieved from a database

// database connection code omitted for brevity 

$author = 'Sam Clemens'; 
// query a db 
$query = "SELECT quote FROM aphorisms WHERE author like "$author'"; 
$result = mysql_query ($query); 

// write out the results of the query 
if (0 == mysql_num_rows ($result)) {
   die ("Sorry, no witticisms from $author for you today!"); 
} 

while ($temp = mysql_fetch_row ($result)) {
   echo stripslashes ($temp[0]), "\n\n"; 
}

Final Thoughts

stripslashes() removes the backslashes from any escape sequence that it encounters in a string. An escape sequence starts with a backslash and is followed by one or more characters. This function is often used to strip backslashes from data retrieved from a database or to clean up data submitted by an HTML form.

Filed Under: PHP

Some more articles you might also be interested in …

  1. PHP base_convert function – Convert a number between arbitrary bases
  2. PHP function chunk_split() – Split a string into smaller chunks
  3. PHP atan2 function – Arc tangent of two variables
  4. PHP connection_status function – Returns connection status bitfield
  5. PHP function str_replace() – Replace all occurrences of the search string with the replacement string
  6. PHP function strcmp() – Binary safe string comparison
  7. PHP function str_repeat() – Repeat a string
  8. PHP Function str_split() – Convert a string to an array
  9. htaccess Cheatsheet
  10. PHP function bin2hex – Convert binary data into hexadecimal representation

You May Also Like

Primary Sidebar

Recent Posts

  • aws ec2: CLI for AWS EC2 (Command Examples)
  • aws cur – Create, query, and delete AWS usage report definitions (Command Examples)
  • aws configure – Manage configuration for the AWS CLI (Command Examples)
  • aws cognito-idp: Manage Amazon Cognito user pool and its users and groups using the CLI

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright