dongpiao8821 2013-01-04 17:48
浏览 8
已采纳

检查空字符串php

I am trying to write to csv.

With this function I need to cheek if there is nothing in write other value

I have tried with empty, preg_match, strlen but I always get one or another So I need to check in $v->Klasifikacija if it is empty then enter $v->BrojArtikla How Can I do that?

For now I got it working with this but do not understand why:

if (strlen(trim((string)$v->Klasifikacija)) < 4) {

If you can maybe explain this to me?

elseif ($v3 == 'Referenca') {
    if (preg_match('/\S/', $v->Klasifikacija)) {
        $b .= $v->BrojArtikla . DELIMITER;
        $v->$v3 = $v->BrojArtikla;
    } else {
        $b .= $v->Klasifikacija . DELIMITER;
        $v->$v3 = $v->Klasifikacija;
    }
}

This is whole code for pasring XML file

            function parsing_xml($supplier_name,$xml_url,$opsi='string'){
                $xml = simplexml_load_file(utf8_encode($xml_url), 'SimpleXMLElement', LIBXML_NOCDATA);
                //$xml = simplexml_load_file($xml_url, 'SimpleXMLElement', LIBXML_NOCDATA);
                if(!$xml){echo "xml $xml_url not loaded.";exit;}
                $i=0;
                $string="";
                foreach($xml as $k=>$v){
                    $b="";
                    if($i==0){
                        foreach($v as $k1=>$v1){
                            $head[]=$k1;
                        }
                        array_push($head,"dugi_opis","meta_description","meta_tag","tax","supplier","quantity","price","Referenca");
                        //array_push($head,"dugi_opis","meta_description","meta_tag","tax","supplier","quantity","price","Referenca");
                        foreach($head as $k4=>$v4){
                            $b.= "$v4".DELIMITER;
                        }
                    }//echo $xml->$k->BrojArtikla."=>".$xml->$k->Image."<pre>".print_r($head,1)."</pre>";
                    foreach($head as $k3=>$v3){
                        if($v3=='meta_description'){$v3='Naziv';}

                        if(empty($v->$v3) or $v->$v3=="" or !isset($v->$v3)){
                            $v->$v3="|||";
                        }
                        if($v3=='tax'){
                            $b.=TAX.DELIMITER;
                            $v->$v3=TAX;
                        }elseif($v3=='supplier'){
                            $b.=$supplier_name.DELIMITER;
                            $v->$v3=$supplier_name;
                        }elseif($v3=='quantity'){
                            if($v->Status=="Dostupno"){
                            //if($v->Status=='Raspoloživo'){
                                $b.="2".DELIMITER;
                                $v->$v3=2;
                            }else{
                                $b.="0".DELIMITER;
                                $v->$v3=0;
                            }
                        }elseif($v3=='Akcija'){
                            $b.= (string) floatval($v->$v3).DELIMITER;
                            $v->$v3= (string) floatval($v->$v3);
                        }elseif($v3=='NetoPrice'){
                            $b.= (string) floatval($v->$v3).DELIMITER;
                            $v->$v3= (string) floatval($v->$v3);
                        }elseif($v3=='ListPrice'){
                            $b.= (string) floatval($v->$v3).DELIMITER;
                            $v->$v3= (string) floatval($v->$v3);
                        }elseif($v3=='Rabat'){
                            $b.= (string) floatval($v->$v3).DELIMITER;
                            $v->$v3= (string) floatval($v->$v3);
                        }elseif($v3=='price'){
                            $b.= (string) floatval($v->NetoPrice)*MARZA.DELIMITER;
                            $v->$v3=(string) floatval($v->NetoPrice)*MARZA;
                        }


                        elseif($v3=='meta_tag'){
                            $b.=$v->Klasifikacija.','.$v->Proizvodjac.DELIMITER;
                            $v->$v3=$v->Klasifikacija.','.$v->Proizvodjac;
                        }


                        elseif($v3=='Referenca'){
                            //if(empty($v->Klasifikacija)){
                            //if(strlen($v->Klasifikacija)===0){
                            ///if(preg_match('/\S/',$v->Klasifikacija)){

                            //if(isset($v->Klasifikacija) || $v->Klasifikacija === ''){
                            //if(isset($v->Klasifikacija)){
                            //if(strlen($v->Klasifikacija) < 2){

                            if(trim($v->Klasifikacija) === ''){

                            $b.=$v->BrojArtikla.DELIMITER;
                            $v->$v3=$v->BrojArtikla;


                            //$b.=$v->Klasifikacija.DELIMITER;
                           // $v->$v3=$v->Klasifikacija;

                            }else{

                            //$b.=$v->BrojArtikla.DELIMITER;
                            //$v->$v3=$v->BrojArtikla;

                            $b.=$v->Klasifikacija.DELIMITER;
                            $v->$v3=$v->Klasifikacija;


                            }

                        }









                        // ovo za sad nije potrebno
                        // elseif($v3=='dugi_opis'){
                            // $b.=$v->dugi_opis.DELIMITER;
                            // $v->$v3=$v->dugi_opis;
                        // }



                        else{
                            $b.=$v->$v3.DELIMITER;
                        }
                    }
                    $supplier_item[(string)$v->BrojArtikla]=$v;
                    $b=substr($b,0,-1);
                    $b=str_replace("'","\'",$b);
                    $b=str_replace('"','\"',$b);
                    $b=str_replace("
"," ",$b);

                    $i++;
                    $string.="
$b";    

                }
                if($opsi=='string'){
                    return $string;
                }elseif($opsi=='supplier_item'){
                    return $supplier_item;
                }else{
                    $z['string']=$string;
                    $z['supplier_item']=$supplier_item;
                    return  $z;
                }
            }
  • 写回答

3条回答 默认 最新

  • douniao7308 2013-01-04 18:00
    关注

    What is the type of $v->Klasifikacija? If it's not a string (it could be a SimpleXMLElement instance), then maybe you should cast it to a string and check for emptyness afterwards. Try var_dump($v->Klasifikacija) to determine the type. Maybe your having multiple problems here. You could do something like

    $stringValue = (string) $v->Klasifikacija;
    $stringValue = trim($stringValue);
    if (strlen($stringValue) == 0)) {
        // ...
    }
    

    Also keep in mind that empty is true for "0"!

    By the way, you can safe a lot of time using PHPs CSV functions!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历