Mar
11
2009

Removing Extraneous Backslashes with PHP

by   |  Posted in Tutorials  |  2 comments

This is something that most people who use PHP already know. I still think it is useful to mention it because some less experienced PHP coders might not have come across this function just yet and they will be grateful.

If you are displaying a string using PHP, sometimes you see something that looks like this, “I was over a my sister\’s place to celebrate my father\’s birthday…”. That is because PHP actually uses quotes for certain functions so when it encounters a quote in a string, is places the backslash “\” before it to signify that is should not be read as code, but as text. This usually happens with textarea boxed and can be easily avoided. extraneous

<?php stripslashes($string); ?>

Now your string will display like this, “I was over a my sister’s place to celebrate my father’s birthday…”.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , , , , ,

Short URL: http://bit.ly/a7nhmM

Discussion 2 Comments

  1. Brandon on November 4, 2010 at 10:25 pm

    Just out of curiosity, if I running a WordPress site, where do I place this line of code? Is it in the wp-config?

    • c.bavota on November 5, 2010 at 12:20 pm

      Do not add it to wp-config.php. This function should wrap whatever text or string you plan on displaying that might have additional slashes. Both WP functions that display your content already have this function built in.