Funkcija explode () je dokaj preprosta, vendar zelo uporabna in se stalno uporablja pri kodiranju. Če ta funkcija ni uporabna, se uporabljajo regularni izrazi, vendar delujejo počasneje, zato je koristno delo s pripravljenimi funkcijami php za pospešitev programa.
Funkcija prevzame niz kot vhod, razdeli ga z delilnikom in dodeli vrednosti nizu s številskimi tipkami.
Z uporabo datoteke dxf autocad kot primer, poskusimo izvleči koordinate in narisati risbo z uporabo knjižnice GD php.
Datoteka dxf je v bistvu dokument z navadnim besedilom, v katerem so zapisane koordinate vseh risb. Izgleda takole:
ENTITIES so entitete, LWPOLYLINE je zaprt poligon, število 10 pa koordinate x, ki mu sledi vrednost (tj. X = 0.0), število 20 pa y, njegova vrednost je iz naslednje vrstice (tj. Y = 500.0) ), nič zapletenega.
Poiščite potrebne informacije v datoteki dxf. Za to uporabimo funkcijo strchr (), ki bo našla entitete (ENTITIES) in poligone (LWPOLYLINE):
$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;
Funkcija eksplodira php bo pomagala ekstrahirati koordinate. V tem primeru uporabljamo samo poligon, vendar je na risbah veliko drugih številk, zato jih dodeljujemo matriki. Upoštevamo samo LWPOLYLINE:
$entities_array = array("LWPOLYLINE");
Naslednji primer najde položaje vseh LWPOLYLINE v dokumentu dxf besedila:
$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }
{$positions_entities[$lastPos] = $entitie;$lastPos = $lastPos + strlen($entitie);}$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }
{$entities_string_array[$positions_entities[$array_keys_positions[$i]]][$array_keys_positions[$i]] = substr($entitie_string, $array_keys_positions[$i], $array_keys_positions[$i+1] - $array_keys_positions[$i] );}$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }
{$entities_string_array[$positions_entities[$array_keys_positions[$i]]][$array_keys_positions[$i]] = substr($first_entitie_string, $array_keys_positions[$i] );}$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }
Vse vrstice mnogokotnikov so zdaj v nizu, tj. Imamo tri poligone, kar pomeni tri ključe in tri vrednosti. Vrednosti vsebujejo vrstice z informacijami iz datoteke dxf o teh poligonih. Zdaj morate razdeliti te črte, da izberete koordinate iz njih. Če želite to narediti, uporabite explode ():
foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }
{$explode_value_e[$key_ent][$key_val] = explode(PHP_EOL, $value_e);}foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }
Iz pridobljene večdimenzionalne matrike dobimo vrednosti, ki sledijo oznakam 10 (x) in 20 (y):
$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);
{$coordinates[$k][] = $coordinates_db[] = $coordinates_x[] = (float)$explode_key[$key+1];}$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);
{$coordinates[$k][] = $coordinates_db[] = $coordinates_y[] = (float)$explode_key[$key+1];}$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);
Niz koordinat $ vsebuje koordinate poligonov, za koordinacijo vrednosti x in y v bazo podatkov bo uporabljen $ koordinat_db, za določitev širine in višine naše risbe v knjižnici GD php pa sta potrebna $ koordinate_x in $ koordinate_y.
Za shranjevanje v bazi podatkov zbiramo koordinate v niz z uporabo implode (), t. Če shranite vsako točko v ločeno celico, bo baza podatkov postopoma rasla do ogromne velikosti. Ustvarite tabelo v MySQL ne bo, da ne bi zapletla kodo.
$implode_coordinates_db[$k] = implode(';',$coordinates_db);
Še vedno je treba napisati funkcijo, ki bo risala sliko v knjižnici GD:
$image_array = array($implode_coordinates_db, $width, $height);
Za ustvarjanje slike potrebujemo koordinate (uporabili bomo niz s koordinatami, ki jih lahko pridobimo iz baze podatkov $ implode_coordinates_db), širino in višino.
Klic funkcije:
$image_base64 = image_create($image_array);
In sama funkcija:
function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}
{$implode_coordinates = $image_array[0];$width = $image_array[1];$height = $image_array[2];$im = imagecreatetruecolor($width+1, $height+1);$white=imagecolorallocate($im,255,255,255);$black = imagecolorallocate($im, 0, 0, 0);imagefilledrectangle($im, 0, 0, $width, $height, $white);foreach( $implode_coordinates as $key =>$coordinates_string ){$coordinates[$key] = explode(';', $coordinates_string);$polygon_arrays_count[$key]=count($coordinates[$key])/2;imagepolygon($im, $coordinates[$key], $polygon_arrays_count[$key] , $black);}function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}
Funkcija prejme koordinate nizov iz baze podatkov, nato pa se linija razdeli z uporabo eksplodirajočega php, po katerem slikovni poligon () nariše poligone.
$coordinates[$key] = explode(';', $coordinates_string);imagepolygon($im, $coordinates[$key], $polygon_arrays_count[$key] , $black);
V tem primeru je bila slika ustvarjena v formatu imagepng base64_encode, sliko pa lahko shranite v jpg ali png.
Zdaj lahko prikažete sliko v brskalniku:
$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src="'data:image/png;base64,'.$image_base64;echo" " ";
Ozadje bo videti tako:
Če je mogoče uporabiti vgrajene funkcije namesto regularnih izrazov, bo proces izvedbe programa trajal malo manj časa, zato je vedno smiselno, da se najprej poskuša doseči cilj s pomočjo že pripravljenih rešitev v php.
Vsa koda:
$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src="'data:image/png;base64,'.$image_base64;echo" " ";
{$positions_entities[$lastPos] = $entitie;$lastPos = $lastPos + strlen($entitie);}$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo " ";
{$entities_string_array[$positions_entities[$array_keys_positions[$i]]][$array_keys_positions[$i]] = substr($entitie_string, $array_keys_positions[$i], $array_keys_positions[$i+1] - $array_keys_positions[$i] );}$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo " ";
{$entities_string_array[$positions_entities[$array_keys_positions[$i]]][$array_keys_positions[$i]] = substr($entitie_string, $array_keys_positions[$i] );}$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo " ";
{$explode_value_e[$key_ent][$key_val] = explode(PHP_EOL, $value_e);}$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo " ";
{$coordinates[$k][] = $coordinates_db[] = $coordinates_x[] = (float)$explode_key[$key+1];}$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo " ";
{$coordinates[$k][] = $coordinates_db[] = $coordinates_y[] = (float)$explode_key[$key+1];}$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo " ";
{$implode_coordinates = $image_array[0];$width = $image_array[1];$height = $image_array[2];$im = imagecreatetruecolor($width+1, $height+1);$white=imagecolorallocate($im,255,255,255);$black = imagecolorallocate($im, 0, 0, 0);imagefilledrectangle($im, 0, 0, $width, $height, $white);foreach( $implode_coordinates as $key =>$coordinates_string ){$coordinates[$key] = explode(';', $coordinates_string);$polygon_arrays_count[$key]=count($coordinates[$key])/2;imagepolygon($im, $coordinates[$key], $polygon_arrays_count[$key] , $black);}$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo " ";