Funkce explode () je poměrně jednoduchá, ale velmi užitečná a používá se nepřetržitě při kódování. Pokud tato funkce není použitelná, používají se regulární výrazy, ale pracují pomaleji, takže je užitečné pracovat s připravenými funkcemi php, které urychlí váš program.
Funkce vezme řetězec jako vstup, rozdělí jej dělitel a přiřadí hodnoty pole s číselnými klávesami.
Použijeme-li jako příklad soubor dxf autocad, pokusme se extrahovat souřadnice a nakreslit výkres pomocí GD Library php.
Soubor dxf je v podstatě dokument prostého textu, ve kterém jsou zaznamenány souřadnice všech výkresů. Vypadá to takto:
ENTITIES jsou entity, LWPOLYLINE je uzavřený polygon, číslo 10 označuje souřadnici x, následuje hodnota (tj. X = 0.0), číslo 20 označuje y, jeho hodnota je od dalšího řádku (tj. Y = 500.0 ), nic složitého.
Najděte potřebné informace v souboru dxf. K tomu používáme funkci strchr (), která nalezne entity (ENTITIES) a polygony (LWPOLYLINE):
$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;
Funkce exploduje php pomáhá extrahovat souřadnice. V tomto příkladu používáme pouze mnohoúhelník, ale na výkresech je mnoho dalších čísel, takže je přidáváme do pole. Zvažujeme pouze LWPOLYLINE:
$entities_array = array("LWPOLYLINE");
Následující příklad najde polohy všech položek LWPOLYLINE v dokumentu dxf text:
$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 }
Všechny řádky polygonů jsou nyní v poli, tzn. Máme tři polygony, což znamená tři klíče a tři hodnoty. Hodnoty obsahují řádky s informacemi z souboru dxf o těchto polygonech. Nyní je třeba rozdělit tyto čáry na výběr souřadnic z nich. Chcete-li to provést, použijte funkci 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 ) }
Z získaného vícerozměrného pole získáváme hodnoty, které následují značky 10 (x) a 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);
$ Souřadnice pole obsahuje souřadnice polygonů, $ coordinates_db se použije pro zápis hodnot x a y do databáze a $ coordinates_x a $ coordinates_y jsou potřebné k určení šířky a výšky našeho výkresu v knihovně GD php.
Pro uložení do databáze shromáždíme souřadnice do řetězce pomocí implode (), t. Pokud uložíte každý bod do samostatné buňky, bude databáze postupně narůstat na obrovskou velikost. Vytvoření tabulky v MySQL nebude tak, aby komplikuje kód.
$implode_coordinates_db[$k] = implode(';',$coordinates_db);
Zbývá napsat funkci, která nakreslí obrázek v knihovně GD:
$image_array = array($implode_coordinates_db, $width, $height);
Chcete-li vytvořit obrázek, potřebujeme souřadnice (použijeme řetězec se souřadnicemi, které lze načíst z databáze $ implode_coordinates_db), šířku a výšku.
Volání funkce:
$image_base64 = image_create($image_array);
A samotná funkce:
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;}
Funkce získává souřadnice řetězce z databáze, pak se řetězec rozdělí pomocí exploze php, po níž imagepolygon () nakreslí naše polygony.
$coordinates[$key] = explode(';', $coordinates_string);imagepolygon($im, $coordinates[$key], $polygon_arrays_count[$key] , $black);
V tomto příkladu byl obrázek vytvořen ve formátu imagepng base64_encode, ale obrázek můžete uložit do formátu jpg nebo png.
Nyní můžete zobrazit obrázek v prohlížeči:
$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src="'data:image/png;base64,'.$image_base64;echo" " ";
Obrázky na pozadí budou vypadat takto:
Pokud je možné použít namísto regulárních výrazů vestavěné funkce, proces provádění bude trvat o něco méně času, takže vždycky dává smysl nejprve pokusit se o dosažení cíle pomocí hotových řešení v php.
Celý kód:
$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 " ";