Home > Article > Backend Development > How to remove '//' character from string in php
Two removal methods: 1. Use the str_replaceh() function to search for the "//" substring in the string and replace it with an empty character. The syntax is "str_replace("//","" , string)". 2. Use the preg_replace() function and regular expression to match the "//" substring and replace it with a null character. The syntax is "preg_replace("/\//","", string)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Method 1: Use str_replaceh() The function removes the "//" characters from the string
Use str_replaceh() to search for the "//" substring and replace it with an empty character.
<?php header('content-type:text/html;charset=utf-8'); $str= '123//34kh9//8jjhg//'; echo "原字符串:".$str."<br>"; $newStr=str_replace("//","",$str); echo "新字符串:".$newStr; ?>
Method 2: Use the preg_replace() function and regular expressions to remove the "//" characters in the string
Use regular expressions to search for all "//
" in the string and replace them with the empty characters ''
.
Regular expression used:
/\//
<?php header('content-type:text/html;charset=utf-8'); $str= '//123//34kh9//8jjhg//'; echo "原字符串:".$str."<br>"; $regex = "/\//"; $newStr=preg_replace($regex,"", $str); echo "新字符串:".$newStr; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove '//' character from string in php. For more information, please follow other related articles on the PHP Chinese website!