How to add backslashes in front of a specified characters in a string with PHP

3 Answers

0 votes
$str = "php c c++ java";

$str = addcslashes($str, 'c');

echo $str;




/*
run:

php \c \c++ java

*/

 



answered Sep 25, 2023 by avibootz
0 votes
$str = "php 9 c 17 c++ 20 java 21";

$str = addcslashes($str, '0..9');

echo $str;




/*
run:

php \9 c \1\7 c++ \2\0 java \2\1

*/

 



answered Sep 25, 2023 by avibootz
0 votes
$str = "php 9 c 17 c++ 20 java 21 ada 2012";

$str = addcslashes($str, 'a..d');

echo $str;




/*
run:

php 9 \c 17 \c++ 20 j\av\a 21 \a\d\a 2012

*/

 



answered Sep 25, 2023 by avibootz
...