The pre-defined character are:
- Single Quote (‘).
- Double Quotes (“)
- Backslash (\)
Note: The PHP
The PHP addslashes Function expects only one parameter.
- $string: The only parameter to the function is a string which you need to escape. In other words, this parameter specifies the string in which we want to add backslashes in front of the pre-defined characters.
The function returns a string after escaping it with backslashes in front of all the pre-defined characters.
Input: addslashes("O'Reilly?") Output: O\'Reilly? Input: addslashes('Bringing "Knowledge" Together') Output: Bringing \"Knowledge\" Together
Let’s discuss a few examples of using the PHP addslashes.
Example 1: Escaping Single Quote
For instance, consider a string with a single quote.
<?php $testString = "O'Reilly?"; $escapedString = addslashes($testString); ?>
The final string after escaping the single quote will be:
Example 2: Escaping Double Quotes
Similarly, we can escape a string containing double quotes.
<?php $testString = 'Bringing "Knowledge" Together'; $escapedString = addslashes($testString); ?>
OUTPUT: Bringing \"Knowledge\" Together
In this article, we discussed the PHP
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.