Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Simple Text to HTML ASCII?

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
No one?
 
0
Close
PHP:
function strtoascii($text) {
    $output = "";
    for ($i=0;$i<strlen($text);$i++)
        $output .= "&#".ord(substr($text,$i,1)).";";
    return $output;
}
 
0
Close
Oh, wow. Thanks Jim!
 
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back