Is there a function in PHP (I can't find one), that given a character, will return the ascii value for that character?

For example, 'a' is given as & #97;

So

<?
$var = 'a';

echo ConvertToAsc($var);

?>

would return & #97;
On which I could then use substr to get out 97 of course.

The reason I ask is I'm looping though names and chopping off the end of ones which are to long.
But as some letters are bigger than others, like capitals, each capital letter counts as 1.5 in my counting algorithm.

I can do it, but currently I'm having to use

<?

function converttoASCII ($var)
{
	switch ($var)
	{
		case 'a' : return '97';
		case 'b' : return '98';
		case 'c' : return '99';
		case 'd' : return '100';
		case 'e' : return '101';
		case 'f' : return '102';
		case 'g' : return '103';
		case 'h' : return '104';
		case 'i' : return '105';
		case 'j' : return '106';
		case 'k' : return '107';
		case 'l' : return '108';
		case 'm' : return '109';
		case 'n' : return '100';
		case 'o' : return '111';
		case 'p' : return '112';
		case 'q' : return '113';
		case 'r' : return '114';
		case 's' : return '115';
		case 't' : return '116';
		case 'u' : return '117';
		case 'v' : return '118';
		case 'w' : return '119';
		case 'x' : return '120';
		case 'y' : return '121';
		case 'z' : return '122';
		default : return 'Fail';
	}
}

Which, I'd rather avoid.

Thanks

    Well - quick search on the opposite of this function in the manual for the opposite function [MAN]chr/MAN will tell you that this compliments the [MAN]ord/MAN function.

    So - the [MAN]ord/MAN function will take your character and return an ascii value for you

      Odd that I couldn't find it, especially as I went though the manual (offline pdf one) looking for ASCII.

      Thanks though, much appreciated.

        Write a Reply...