preg_replace返回一个没有错误的空字符串

时间:2015-07-01 14:21:57

标签: php string preg-replace

所以有我的问题:

$var = "93 Avenue d'Aubière";

我在这个函数中使用了这个$ var:

function stripAccents($str) {
        $str = preg_replace('/[\x{00C0}\x{00C1}\x{00C2}\x{00C3}\x{00C4}\x{00C5}]/u','A', $str);
        $str = preg_replace('/[\x{0105}\x{0104}\x{00E0}\x{00E1}\x{00E2}\x{00E3}\x{00E4}\x{00E5}]/u','a', $str);
        $str = preg_replace('/[\x{00C7}\x{0106}\x{0108}\x{010A}\x{010C}]/u','C', $str);
        $str = preg_replace('/[\x{00E7}\x{0107}\x{0109}\x{010B}\x{010D}}]/u','c', $str);
        $str = preg_replace('/[\x{010E}\x{0110}]/u','D', $str);
        $str = preg_replace('/[\x{010F}\x{0111}]/u','d', $str);
        $str = preg_replace('/[\x{00C8}\x{00C9}\x{00CA}\x{00CB}\x{0112}\x{0114}\x{0116}\x{0118}\x{011A}]/u','E', $str);
        $str = preg_replace('/[\x{00E8}\x{00E9}\x{00EA}\x{00EB}\x{0113}\x{0115}\x{0117}\x{0119}\x{011B}]/u','e', $str);
        $str = preg_replace('/[\x{00CC}\x{00CD}\x{00CE}\x{00CF}\x{0128}\x{012A}\x{012C}\x{012E}\x{0130}]/u','I', $str);
        $str = preg_replace('/[\x{00EC}\x{00ED}\x{00EE}\x{00EF}\x{0129}\x{012B}\x{012D}\x{012F}\x{0131}]/u','i', $str);
        $str = preg_replace('/[\x{0142}\x{0141}\x{013E}\x{013A}]/u','l', $str);
        $str = preg_replace('/[\x{00F1}\x{0148}]/u','n', $str);
        $str = preg_replace('/[\x{00D2}\x{00D3}\x{00D4}\x{00D5}\x{00D6}\x{00D8}]/u','O', $str);
        $str = preg_replace('/[\x{00F2}\x{00F3}\x{00F4}\x{00F5}\x{00F6}\x{00F8}]/u','o', $str);
        $str = preg_replace('/[\x{0159}\x{0155}]/u','r', $str);
        $str = preg_replace('/[\x{015B}\x{015A}\x{0161}]/u','s', $str);
        $str = preg_replace('/[\x{00DF}]/u','ss', $str);
        $str = preg_replace('/[\x{0165}]/u','t', $str);
        $str = preg_replace('/[\x{00D9}\x{00DA}\x{00DB}\x{00DC}\x{016E}\x{0170}\x{0172}]/u','U', $str);
        $str = preg_replace('/[\x{00F9}\x{00FA}\x{00FB}\x{00FC}\x{016F}\x{0171}\x{0173}]/u','u', $str);
        $str = preg_replace('/[\x{00FD}\x{00FF}]/u','y', $str);
        $str = preg_replace('/[\x{017C}\x{017A}\x{017B}\x{0179}\x{017E}]/u','z', $str);
        $str = preg_replace('/[\x{00C6}]/u','AE', $str);
        $str = preg_replace('/[\x{00E6}]/u','ae', $str);
        $str = preg_replace('/[\x{0152}]/u','OE', $str);
        $str = preg_replace('/[\x{0153}]/u','oe', $str);
        $str = preg_replace('/[\x{0022}\x{0025}\x{0026}\x{0027}\x{00A1}\x{00A2}\x{00A3}\x{00A4}\x{00A5}\x{00A6}\x{00A7}\x{00A8}\x{00AA}\x{00AB}\x{00AC}\x{00AD}\x{00AE}\x{00AF}\x{00B0}\x{00B1}\x{00B2}\x{00B3}\x{00B4}\x{00B5}\x{00B6}\x{00B7}\x{00B8}\x{00BA}\x{00BB}\x{00BC}\x{00BD}\x{00BE}\x{00BF}]/u',' ', $str);
        return $str;
    }

如果我使用$ var,则返回一个空字符串,但如果我使用“93 Avenue d'Aubière”作为参数,则返回正确的字符串。

我尝试使用preg_last_error来检查是否有任何错误,但它返回0表示没有错误。

我正在连接我的数据库:

$db = new PDO('mysql:host=localhost;dbname=somedb;charset=utf8', 'username', 'password');

获取这样的数据:

$sqlSelectCommandeExapaq = "SELECT * FROM commande_exapaq WHERE statut = 'en cours'";
$res = $db->query($sqlSelectCommande);
$arrayResCmd = $res->fetchAll();

然后我将$ arrayResCmd传递给了这个函数:

public static function generateInterfaceFile($orders_array)
    {
        // Init file
        $record = new DPDStation();
        // Loop through each order
        foreach ($orders_array as $order_data)
        {
            // Add data to file
            $record->add($order_data['customer_adress'], 0, 35);                                                                
        }
        return $record;
    }

还有DPDStation构造器:

function __construct() {
        $this->line = str_pad("", 1634);
        $this->contenu_fichier = '';
    }

添加功能:

function add($txt, $position, $length) {
        $txt = $this->stripAccents($txt);
        $this->line = substr_replace($this->line, str_pad($txt, $length), $position, $length);
    }

使用以下内容将内容添加到文件中:

$dpd = new DPDStation();
    $record = $dpd->generateInterfaceFile($arrayResCmd);
    file_put_contents($filename, '$VERSION=110'."\r\n", FILE_APPEND);
    file_put_contents($filename, $record->contenu_fichier."\r\n", FILE_APPEND);

因为没有任何内容添加到文件中,所以我查看了stripAccents,问题似乎来自它。

感谢您的帮助:)

0 个答案:

没有答案