0

我有以下数组:

[0] => Array
        (
            [0] => 3,38 m
            [1] => 13,30 s
            [2] => 5,41 m
            [3] => ESE
            [4] => 294º
            [5] => 32,76 km/h
            [6] => W
            [7] => 266º
            [8] => 16,27 ºC
            [9] => 12,80 ºC
            [10] => 0
        )

我想在将数据添加到数据库之前对其进行清理。

此功能几乎存在,但不会删除特殊字符:

function cleanUp(&$value,$key)
{
    $cleaner2 = array("km/h"," ","m","s","º","ºC");
    $value = str_replace($cleaner2, "", $value);
}
array_walk($newArray[0],"cleanUp");

我已经研究过对数组进行编码,但我不确定它现在有什么编码?我可以修剪数组值,但觉得这很不雅。

有任何想法吗?

解决方案: 我从标题中省略了字符集!

header('Content-type: application/json; charset=UTF-8');

这使我的简单 cleanUp 函数可以工作,因为它删除了 Â,然后匹配以下 cleanUp 数组值:

$cleaner2 = array("km/h"," ","m","s","º","ºC","C");
4

2 回答 2

1

你可以试试

$data = array(
  0 => '3,38 m',
  1 => '13,30 s',
  2 => '5,41 m',
  3 => 'ESE',
  4 => '294º',
  5 => '32,76 km/h',
  6 => 'W',
  7 => '266º',
  8 => '16,27 ºC',
  9 => '12,80 ºC',
  10 => 0,
);

$c =  array("km/h"," ","m","s","º","ºC");
$data = array_map(function($v)use($c) {return mb_replace($c,"",$v);},$data);
var_dump($data);

输出

array (size=11)
  0 => string '3,38' (length=4)
  1 => string '13,30' (length=5)
  2 => string '5,41' (length=4)
  3 => string 'ESE' (length=3)
  4 => string '294' (length=3)
  5 => string '32,76' (length=5)
  6 => string 'W' (length=1)
  7 => string '266' (length=3)
  8 => string '16,27C' (length=6)
  9 => string '12,80C' (length=6)
  10 => string '0' (length=1)

功能使用

function mb_replace($search, $replace, $subject, &$count=0) {
    if (!is_array($search) && is_array($replace)) {
        return false;
    }
    if (is_array($subject)) {
        // call mb_replace for each single string in $subject
        foreach ($subject as &$string) {
            $string = &mb_replace($search, $replace, $string, $c);
            $count += $c;
        }
    } elseif (is_array($search)) {
        if (!is_array($replace)) {
            foreach ($search as &$string) {
                $subject = mb_replace($string, $replace, $subject, $c);
                $count += $c;
            }
        } else {
            $n = max(count($search), count($replace));
            while ($n--) {
                $subject = mb_replace(current($search), current($replace), $subject, $c);
                $count += $c;
                next($search);
                next($replace);
            }
        }
    } else {
        $parts = mb_split(preg_quote($search), $subject);
        $count = count($parts)-1;
        $subject = implode($replace, $parts);
    }
    return $subject;
}

功能信用:秋葵

于 2012-11-11T09:13:26.530 回答
0
function __clean( $text )
{
    $buff = implode('[:#:]',$text);
    $buff = str_replace("","UNICODE OF $text",$buff);
    return ($buff); 
}
于 2012-11-11T09:18:58.853 回答