ZaZaKi, a web developer Between Manchester UK & Rotterdam NL. © 2015-2024.

SVG get SVG icon dynamically

function svg_icon($svg_iconName = "", $svg_iconColor = "black"){
	
	$pathtofile = 'icons/'. $svg_iconName.'.svg';  
	
	if (file_exists( $pathtofile)) {   
		$svg_file = file_get_contents($pathtofile);
		$svg_fileremoved = substr($svg_file, 19);
		$svg_fileremoved = "<svg fill='".$svg_iconColor."'" . $svg_fileremoved;
		echo $svg_fileremoved;
	}else{
		echo "SVG not Found";
	}  
} 

The SVG file must start with

 <svg fill="#000000" 

example of SVG file

<svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" height="512" width="512">
<g>
<path fill="none" d="M0 0h24v24H0z"></path>
<path d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z"></path>
</g>
</svg>

Call the function:
First parameter is name of the SVG icon whiout file extenstion and second parameter is color of the icon

echo svg_icon("home", "blue");