Simple Text to HTML ASCII?

SpaceshipSpaceship
SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
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
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

A browser tab with a favicon next to the page title
Search members
You can now search for members by name, company, location and more. Open the search at the top of any page and choose Members. You will...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back