Funkcija explode () je prilično jednostavna, ali iznimno korisna i koristi se kontinuirano u kodiranju. Ako ova funkcija nije primjenjiva, koriste se regularni izrazi, ali oni rade sporije, tako da je korisno raditi s gotovim php funkcijama kako biste ubrzali svoj program.
Funkcija uzima niz kao ulaz, razbija ga djeliteljem i dodjeljuje vrijednosti nizu numeričkim tipkama.
Koristeći primjer dxf autocad, pokušajmo izvući koordinate i nacrtati crtež pomoću GD knjižnice php.
Datoteka dxf je u biti tekstualni dokument u kojem se bilježe koordinate svih crteža. Izgleda ovako:
ENTITIES su entiteti, LWPOLYLINE je zatvoreni poligon, broj 10 označava x koordinatu, a slijedi vrijednost (tj. X = 0.0), broj 20 označava y, njegova vrijednost je iz sljedećeg retka (tj. Y = 500.0 ), ništa komplicirano.
Pronađite potrebne informacije u dxf datoteci. Za to koristimo funkciju strchr () koja će pronaći entitete (ENTITIES) i poligone (LWPOLYLINE):
$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;
Funkcija eksplodira php pomoći će izdvojiti koordinate. U ovom primjeru koristimo samo poligon, ali na crtežima ima mnogo drugih brojki, tako da ih dodijelimo nizu. Smatramo samo LWPOLYLINE:
$entities_array = array("LWPOLYLINE");
Sljedeći primjer pronalazi pozicije svih LWPOLYLINE u tekstu dxf dokumenta:
$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 }
Sve linije poligona sada su u nizu, tj. Imamo tri poligona, što znači tri ključa i tri vrijednosti. Vrijednosti sadrže retke s informacijama iz dxf datoteke o tim poligonima. Sada morate razdvojiti ove linije kako biste odabrali koordinate iz njih. Da biste to učinili, upotrijebite 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 dobivenog višedimenzionalnog niza dobivamo vrijednosti koje slijede oznake 10 (x) i 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 koordinata $ koordinata sadrži koordinate poligona, $ koordinate_db će se koristiti za zapisivanje x i y vrijednosti u bazu podataka, a $ koordinate_x i $ koordinate_y potrebne su za određivanje širine i visine našeg crteža u GD php knjižnici.
Za pohranu u bazi podataka, skupljamo koordinate u niz pomoću implode (), t. Ako svaku točku pohranite u zasebnu ćeliju, baza podataka će postupno rasti do ogromne veličine. Stvaranje tablice u MySQL neće, kako ne bi komplicirali kod.
$implode_coordinates_db[$k] = implode(';',$coordinates_db);
Ostaje napisati funkciju koja će nacrtati sliku u GD knjižnici:
$image_array = array($implode_coordinates_db, $width, $height);
Za kreiranje slike potrebne su nam koordinate (koristit ćemo niz s koordinatama koje se mogu dohvatiti iz baze podataka $ implode_coordinates_db), širine i visine.
Poziv funkcije:
$image_base64 = image_create($image_array);
I 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 dobiva koordinate niza iz baze podataka, a zatim se string razdvaja pomoću eksplodirajućeg php-a, nakon čega nas slikapolygon () vuče poligone.
$coordinates[$key] = explode(';', $coordinates_string);imagepolygon($im, $coordinates[$key], $polygon_arrays_count[$key] , $black);
Za ovaj primjer, slika je stvorena u formatu imagepng base64_encode, ali sliku možete spremiti u jpg ili png.
Sada možete prikazati sliku u pregledniku:
$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src="'data:image/png;base64,'.$image_base64;echo" " ";
Pozadinske slike izgledat će ovako:
Ako je moguće koristiti ugrađene funkcije umjesto regularnih izraza, proces izvođenja programa će potrajati malo manje vremena, tako da uvijek ima smisla najprije pokušati postići cilj uz pomoć gotovih rješenja u php-u.
Svi kodovi:
$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 " ";