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 an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.