Viewed   108 times

Say I have a string in php, that prints out to a text file like this:

nÖ§9q1Fª£

How do I get the byte codes of this to my text file rather than the funky ascii characters?

 Answers

2

Use the ord function

http://ca.php.net/ord

eg.

<?php
$var = "nÖ§9q1Fª£ˆæÓ§Œ_»—Ló]j";

for($i = 0; $i < strlen($var); $i++)
{
   echo ord($var[$i])."<br/>";
}
?>
Sunday, August 7, 2022
5

Here is what you can do :

echo changeLetter("tä[email protected]", 'ä', 'ae'), PHP_EOL;
echo changeLetter("tü[email protected]", 'ü', 'ue'), PHP_EOL;
echo changeLetter("tö[email protected]", 'ö', 'oe'), PHP_EOL;

Output

[email protected]
[email protected]
[email protected]

Function Used

function changeLetter($str, $needle, $val, $pos = 0) {
    $next = function ($str, &$pos) {
        if (! isset($str[$pos]))
            return false;
        $char = ord($str[$pos]);
        if ($char < 128) {
            return $str[$pos ++];
        } else {
            if ($char < 224) {
                $bytes = 2;
            } elseif ($char < 240) {
                $bytes = 3;
            } elseif ($char < 248) {
                $bytes = 4;
            } elseif ($char = 252) {
                $bytes = 5;
            } else {
                $bytes = 6;
            }
            $str = substr($str, $pos, $bytes);
            $pos += $bytes;
            return $str;
        }
    };
    $mstr = "";
    while(($chr = $next($str, $pos)) !== false) {
        $mstr .= $chr == $needle ? $val : $chr;
    }
    return $mstr;
}
Wednesday, August 24, 2022
1

The code:

public class Test {
    public static void main(String args[]) {
        String string = args[0];
        System.out.println("last character: " +
                           string.substring(string.length() - 1)); 
    }
}

The output of java Test abcdef:

last character: f
Friday, October 21, 2022
 
mhstnsc
 
2

That's what happens when someone sends you an email in outlook, so watch out, because you're totally missing the opening pair of those single quotes, and closing pair for the double quotes. But really, you need to be using html entity codes.

Why? Because there's a hell of a lot more out there in the wilderness than those two codes you posted above, in-fact, you've only shown half the set of quotes (ie, you missed the closing single quote, and opening double quote). There's hundreds, you need to be encoding them in a better way than string replace.

There's a couple ways to manage the translation from encoding to html entity.

http://php.net/manual/en/function.htmlentities.php

http://piology.org/entities.html

http://konieczny.be/unicode.html

There's more also, but they basically do the same thing.

Wednesday, August 31, 2022
 
stucki
 
2

Add your string as a watch, then edit the watch expression and append ".ToCharArray()" to view it as an array of chars. When you expand your watch you will see char code next to each individual char. Checking "Hexadecimal display" will show you hex codes for each character.

Wednesday, December 14, 2022
 
hpierce
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :