Home > Article > Backend Development > How to convert underscore to comma in php
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)".
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 = '1_2_3_44678_5_'; echo str_replace("_",",",$str); ?>
Method 2: Use str_ireplace() function
<?php $str = '1_2_3_44678_5_'; echo $str."<br>"; echo str_ireplace("_",",",$str); ?>
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 = '3_44678_5_'; echo $str."<br>"; echo preg_filter("/_/", ",", $str); ?>
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!