Home  >  Article  >  Backend Development  >  How to remove "//" character from string in php

How to remove "//" character from string in php

青灯夜游
青灯夜游Original
2022-08-01 19:16:032803browse

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)".

How to remove

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str= &#39;123//34kh9//8jjhg//&#39;;
echo "原字符串:".$str."<br>";
$newStr=str_replace("//","",$str);
echo "新字符串:".$newStr;
?>

How to remove // character from string in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str= &#39;//123//34kh9//8jjhg//&#39;;
echo "原字符串:".$str."<br>";
$regex = "/\//";
$newStr=preg_replace($regex,"", $str);
echo "新字符串:".$newStr;
?>

How to remove // character from string in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn