NameSilo

Simple Text to HTML ASCII?

Spaceship Spaceship
Watch

Tree

Established Member
Impact
9
Is there any way to simply convert text to HTML ASCII code? Here's what I currently have, but I think it could be done much easier.

PHP:
function strtoascii($text)
{
	$final_text = '';
	$alpha = array(64 => "@",
	"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
	"[","\\","]","^","_","`",
	"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
	$alpha['46'] = ".";
	for ($i=0;$i<10;$i++)
	{
		$alpha[$i+48] .= $i;
	}
	for ($i=0;$i<strlen($text);$i++)
	{
		$char = substr($text,$i,1);
		$ascii = array_search($char,$alpha);
		if ($ascii === false) 
		{
			$ascii = $char;
		}
		$final_text .= "&#".$ascii.";";
	}
	return $final_text;
}

This works, but the code's a bit confusing...
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
No one?
 
0
•••
PHP:
function strtoascii($text) {
    $output = "";
    for ($i=0;$i<strlen($text);$i++)
        $output .= "&#".ord(substr($text,$i,1)).";";
    return $output;
}
 
0
•••
Oh, wow. Thanks Jim!
 
0
•••

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back