1

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("\n"," ",$b);

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

                }
                if($opsi=='string'){
                    return $string;
                }elseif($opsi=='supplier_item'){
                    return $supplier_item;
                }else{
                    $z['string']=$string;
                    $z['supplier_item']=$supplier_item;
                    return  $z;
                }
            }
8
  • If $v->Klasifikacija is a string have you tried if(strlen($v->Klasifikacija)==0) { ... }? Commented Jan 4, 2013 at 17:49
  • 4
    why don't you use isset or empty Commented Jan 4, 2013 at 17:49
  • if(strlen($v->Klasifikacija)===0){ This also does not work, I have tried empty() also Commented Jan 4, 2013 at 17:51
  • $v->Klasifikacija is and object Commented Jan 4, 2013 at 17:53
  • 1
    Perhaps $v->Klasifikacija and $v->BrojArtikla don't look the way you expect them to. Try print_r( $v ) to make sure. Commented Jan 4, 2013 at 17:54

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

4 Comments

Can I do something like if (strlen(trim((string)$stringValue)) == 0))
I have tried if (strlen(trim((string)$v->Klasifikacija)) == 0) { And still the same
This works But why? if (strlen(trim((string)$v->Klasifikacija)) < 4) {
just check what it contains: var_dump(trim((string)$v->Klasifikacija))
2

The best way to check if a string is empty is to use strlen.

Take care empty returns true for the string "0".

$a = "";
$b = "0";
echo empty($a), " ", empty($b), " ", strlen($a), " ", strlen($b), "\n";
//OUTPUT: 1, 1, 0, 1

If you want to consider strings having only spaces empty, please look at the trim PHP method. Your condition then would be:

if (0 == strlen(trim($v->Klasifikacija))) {}

2 Comments

I have tried it but same result, I cannot get What am I doing wrong here
When I print it I get real values, references
0

if $v->Klasifikacija is a string then Try this....

 elseif($v3=='Referenca'){

        if(empty($v->Klasifikacija)){

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

        $v->$v3=$v->BrojArtikla;

        }else{

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

        $v->$v3=$v->Klasifikacija;

        }

    }

Also you van check $v->Klasifikacija=='' or strlen($v->Klasifikacija)>0 etc....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.