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;
}
}