Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查数组中的字符串是否有特殊字符_Php_Arrays_Special Characters - Fatal编程技术网

Php 检查数组中的字符串是否有特殊字符

Php 检查数组中的字符串是否有特殊字符,php,arrays,special-characters,Php,Arrays,Special Characters,我得到了一个数组,其中包含以下几个字符串: array(133) { [0]=> array(1) { ["row"]=> array(5) { [0]=> string(10) "testd ' /% ata" [1]=> string(14) "testdata 111" [2]=> string(17) "testdata 123" [3]=> string(0) "" [4]=> string(0)

我得到了一个数组,其中包含以下几个字符串:

array(133) {
 [0]=>
  array(1) {
["row"]=>
array(5) {
  [0]=>
  string(10) "testd ' /% ata"
  [1]=>
  string(14) "testdata 111"
  [2]=>
  string(17) "testdata 123"
  [3]=>
  string(0) ""
  [4]=>
  string(0) ""
 }
}
[1]=>
 array(1) {
 ["row"]=>
 array(5) {
  [0]=>
  string(9) "198"
  [1]=>
  string(14) "testdata"
  [2]=>
  string(41) "testdat"
  [3]=>
  string(0) ""
  [4]=>
  string(0) ""
 }
}
我的问题是如何在特殊字符上检查数组中的字符串?这些特殊字符在导入my DB时会导致语法错误

我想我需要用这样的东西

preg_replace('/[^a-zA-Z0-9_ -%][().][\/]/s', '', $String);
有人能帮我吗

好吧,我已经创建了这段代码:

        // search for special chars in the import data and remove them 
        $illegal = "#$%^&*()+=-[]';,./{}|:<>?~";

        foreach ($data_set as $data) 
        foreach ($data_set['data'] as $row) {
           if(strpbrk($row, $illegal)) {
            echo($row);
            die();
           }                
            else {
                //not allowed ,escape or do what you want
                echo("no characters found");
                die();
            } 
            var_dump($row);
            die();
         }
//在导入数据中搜索特殊字符并将其删除
$INLOCALL=“#$%^&*()+=-[];,./{}|:?~”;
foreach($data\u设置为$data)
foreach($data_set['data']作为$row){
if(strpbrk($row,$INLARK)){
echo($世界其他地区);
模具();
}                
否则{
//不允许,逃跑或做你想做的事
echo(“未找到字符”);
模具();
} 
var_转储($row);
模具();
}
但这仍然给出了一个错误:

遇到一个PHP错误 严重性:警告

消息:strpbrk()要求参数1为字符串,数组给定

文件名:controllers/import.php

行号:153

没有找到字符

您可以查看一下。它应该能解决你的问题

例如:

$tests = "testd ' /% ata";
);
$illegal = "#$%^&*()+=-[]';,./{}|:<>?~";

echo (false === strpbrk($test, $illegal)) ? 'Allowed' : "Contains special chars";

将其插入数据库时是否正在转义它?您可能试图解决错误的问题。请使用带占位符的预处理语句检查字符串。。然后呢?从字符串中去掉那些符号?是否从数组中删除这些字符串?或者甚至用它们删除整个数组项?使用mysql_real_escapse_string()函数来转义\、',“好吧,我可能还是个新手。'导致语法错误,我希望防止出现此错误。我认为从数组中的字符串中删除特殊字符可以解决这个问题。如何将此字符应用于数组?我的数组位于变量$data中_set@mastahb检查我的答案
foreach ($yourArray as $array) 
foreach ($array['row'] as $row) {

    if(strpbrk($row, $illegal)) { 
         //allowed ,insert to db
    }
    else {
        //not allowed ,escape or do what you want
    }  

 }