PHP function store elements in an array -
i'm using front end javascript text editor, submits data in html format, convert images base64 encoded format.
the following function parse $_post super global, html content , store encoded images in images folder along appropriate extension.
$html = preg_replace_callback("/src=\"data:([^\"]+)\"/", function ($matches) { list($contenttype, $enccontent) = explode(';', $matches[1]); if (substr($enccontent, 0, 6) != 'base64') { return $matches[0]; } $imgbase64 = substr($enccontent, 6); $imgfilename = md5($imgbase64); // unique filename $imgext = ''; switch($contenttype) { case 'image/jpeg': $imgext = 'jpg'; break; case 'image/gif': $imgext = 'gif'; break; case 'image/png': $imgext = 'png'; break; default: return $matches[0]; } // here i'm able echo image names thier extentions. echo $imgfilename . '.' . $imgext; $imgpath = 'zendesk-images/'.$imgfilename.'.'.$imgext; // save file disk if doesn't exist if (!file_exists($imgpath)) { $imgdecoded = base64_decode($imgbase64); $fp = fopen($imgpath, 'w'); if (!$fp) { return $matches[0]; } fwrite($fp, $imgdecoded); fclose($fp); } return 'src="'.$imgpath.'"'; }, $html);
i'm able echo image names in following line
echo $imgfilename . '.' . $imgext;
i'm trying store converted images file names in array, haven't been successful in doing that.
here have tried, initialize array before function
$images = array();
then instead of echoing, have tried following.
$images[] = $imgfilename . '.' . $imgext;
but didn't work, , end empty array
if want use $images in outer scope need like
$images = array(); $html = preg_replace_callback("/src=\"data:([^\"]+)\"/", function ($matches) use(&$images) { //... $images[] = $imgfilename . '.' . $imgext; //... } echo implode(', ', $images);
Comments
Post a Comment