Home  >  Article  >  Backend Development  >  How to convert underscore to comma in php

How to convert underscore to comma in php

青灯夜游
青灯夜游Original
2022-05-31 17:22:592284browse

3 methods: 1. Use the "str_replace("_",",",$str)" statement to find the underscore and replace it with a comma; 2. Use "str_ireplace("_"," ,",$str)" statement; 3. Use "preg_filter("/_/",",",$str)".

How to convert underscore to comma in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php Convert the underscore to a comma Method: Find the underscore in the string and replace it with a comma.

You can use the following 3 methods to implement search and replacement:

Method 1: Use str_replace() function

<?php
$str = &#39;1_2_3_44678_5_&#39;;
echo str_replace("_",",",$str);
?>

How to convert underscore to comma in php

Method 2: Use str_ireplace() function

<?php
$str = &#39;1_2_3_44678_5_&#39;;
echo $str."<br>";
echo str_ireplace("_",",",$str);
?>

How to convert underscore to comma in php

Description: str_ireplace() and str_replace have similar syntax, and both Use a new string to replace a specific string specified in the original string; but str_replace is case-sensitive, str_ireplace() is not case-sensitive

Method 3: Use preg_replace()

The preg_replace() function can be used with regular expressions to find all underscores and replace them with commas.

<?php
$str = &#39;3_44678_5_&#39;;
echo $str."<br>";
echo preg_filter("/_/", ",", $str);
?>

How to convert underscore to comma in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert underscore to comma 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