Home > Backend Development > PHP Problem > How to remove special symbols from php array

How to remove special symbols from php array

藏色散人
Release: 2023-03-02 08:28:02
Original
3153 people have browsed it

php method to remove symbols: first create a string array; then use the "preg_replace" function to replace the specified symbol with an empty string. The statement is "$arr[$i]=preg_replace(' #\-#','',$arr[$i]);".

How to remove special symbols from php array

Remove special symbols from elements in php array

First of all, your array must be a string array, if not you Use the strval() function to convert each array element into a string,

Then, use the preg_replace function to replace '-' with an empty string, and 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); 
 
?>
Copy after login

Run result:

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

For more related articles, please visit PHP Chinese website!

The above is the detailed content of How to remove special symbols from php array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template