Функцията explode () е доста проста, но изключително полезна и се използва непрекъснато в кодирането. Ако тази функция не е приложима, се използват регулярни изрази, но те работят по-бавно, така че е полезно да се работи с готови php функции за ускоряване на програмата.
Функцията приема низ като вход, разделя го по делител и присвоява стойности на масив с цифрови клавиши.
Използвайки dxf autocad файла като пример, нека се опитаме да извлечем координатите и да начертаем чертеж с помощта на библиотеката GD php.
Файлът dxf е по същество документ от обикновен текст, в който се записват координатите на всички чертежи. Изглежда така:
ENTITIES са единици, LWPOLYLINE е затворен многоъгълник, числото 10 означава x координатата, следвано от стойността (т.е. x = 0.0), числото 20 означава y, неговата стойност е от следващия ред (т.е., y = 500.0 ), нищо сложно.
Намерете необходимата информация в dxf файла. За тази цел използваме функцията strchr (), която ще намери единици (ENTITIES) и полигони (LWPOLYLINE):
$content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;
Функцията експлодира php ще ви помогне да извлечете координатите. В този пример използваме само многоъгълник, но има много други фигури в чертежите, така че ние ги присвояваме на масив. Разглеждаме само LWPOLYLINE:
$entities_array = array("LWPOLYLINE");
Следният пример намира позициите на всички LWPOLYLINE в текстов dxf документ:
$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 }
Всички редове от полигони вече са в масив, т.е. имаме три полигона, което означава три ключа и три стойности. Стойностите съдържат редовете с информация от dxf файла за тези полигони. Сега трябва да разделите тези линии, за да изберете координати от тях. За да направите това, използвайте 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 ) }
От получената многомерна решетка получаваме стойностите, които следват 10 (x) и 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);
Матрицата $ koordins съдържа координатите на многоъгълниците, $ koordinate_db ще се използва за записване на стойностите на x и y в базата данни, а $ koordinate_x и $ koordinate_y са необходими за определяне на ширината и височината на нашия чертеж в GD php библиотеката.
За съхранение в базата данни събираме координатите в низ с помощта на implode (), t. За да запазите всяка точка в отделна клетка, базата данни постепенно ще достигне огромни размери. Създаване на таблица в MySQL няма, така че да не усложняват кода.
$implode_coordinates_db[$k] = implode(';',$coordinates_db);
Остава да напишете функция, която ще нарисува картина в библиотеката на GD:
$image_array = array($implode_coordinates_db, $width, $height);
За да създадем картина, се нуждаем от координати (ще използваме низ с координати, които могат да бъдат извлечени от базата данни $ implode_coordinates_db), ширина и височина.
Извикване на функция:
$image_base64 = image_create($image_array);
И самата функция:
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;}
Функцията получава низови координати от базата данни, след което низът се разделя чрез взривяване php, след което imagepolygon () ни привлича полигони.
$coordinates[$key] = explode(';', $coordinates_string);imagepolygon($im, $coordinates[$key], $polygon_arrays_count[$key] , $black);
За този пример картината е създадена във формат imagepng base64_encode, но можете да запазите картината в jpg или png.
Сега можете да покажете картината в браузъра:
$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src="'data:image/png;base64,'.$image_base64;echo" " ";
Фоновите снимки ще изглеждат така:
Ако е възможно да се използват вградени функции вместо регулярни изрази, процесът на изпълнение на програмата ще отнеме малко по-малко време, така че винаги има смисъл първо да се опитате да постигнете целта с помощта на готови решения в php.
Всички кодове:
$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 " ";