Home  >  Article  >  Backend Development  >  How to remove special symbols from elements in php array

How to remove special symbols from elements in php array

coldplay.xixi
coldplay.xixiOriginal
2020-10-06 11:40:082602browse

How to remove special symbols from elements in a php array: First, the array must be a string array. If not, use the [strval()] function to convert each array element into a string; then use the [preg_replace] function Just replace [-] with an empty string.

How to remove special symbols from elements in php array

How to remove special symbols from elements in a php array:

First of all, your array must be a string array. If not, you want to use the strval() function to convert each array element into a string,

Then, use the preg_replace function to replace '-' with a null character String, that’s it.

The complete php program is as follows:

<?php
 
$arr=array(&#39;2015-01&#39;,&#39;2015-02&#39;,&#39;2015-03&#39;,&#39;2015-04&#39;);
 
for($i=0;$i<count($arr);$i++){    
 
 $arr[$i]=preg_replace(&#39;#\-#&#39;,&#39;&#39;,$arr[$i]);
 
}
 
print_r($arr); 
 
?>

Running result:

Array ( [0] => 201501 [1] => 201502 [2] => 201503 [3] => 201504 )

Related free learning recommendations: php programming (video)

The above is the detailed content of How to remove special symbols from elements in php array. 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