0

I'm using an RTF converter and I need 240 as &#U050&#U052&#U048 but Im not to sure how to do this!?!

I have tried using the following function:

function string_to_ascii($string) {
    $ascii = NULL;
    for ($i = 0; $i < strlen($string); $i++) {
            $ascii += "&#U"+str_pad(ord($string[$i]),3,"0",STR_PAD_LEFT);
    }
    return($ascii);
}

But it still just outputs just the number (e.g. 2 = 50) and ord just makes it go mad.

I've tried echo "-&#U"+ord("2")+"-"; and I get 50416 !?!?

I have a feeling it might have something to do with encoding

6
  • 1
    &#U050 is not ASCII. It's an XML/HTML numeric entity, using unicode encoding. Anyway, what kind of insane software needs plain text characters to be entity encoded like that? Commented Jan 22, 2013 at 14:29
  • paggard.com/projects/rtf.generator :( Commented Jan 22, 2013 at 14:40
  • Hmmm, well, I've not used it, but a quick scan of the documentation only mentions using entities for non ASCII symbols like ©. And other examples seem to use plain text. As I say I've not used it, so if it really does need all that encoding then so be it, but I'd be very surprised. Commented Jan 22, 2013 at 14:51
  • I know. When I use a £ I have to use &#U163. It then won't let me use numbers or letters after it (I suppose till it hits a space). So 240 becomes &#U050&#U052&#U048 :( Commented Jan 24, 2013 at 10:32
  • 1
    Entity codes are supposed to end with a semi-colon -- ie &#U163;. That's how it knows it's finished the entity. Add a semi-colon to the end of your entities, and it let you use numbers and letters after it just fine. Commented Jan 24, 2013 at 10:37

2 Answers 2

1

I think you're over thinking this. Convert the string to an array with str_split, map ord to all of it, then if you want to format each one, use sprintf (or str_pad if you'd like), like this:

function string_to_ascii($string) {
    $array = array_map( 'ord', str_split( $string));
    // Optional formatting:
    foreach( $array as $k => &$v) {
        $v = sprintf( "%03d", $v);
    }
    return "&#U" . implode( "&#U", $array);
}

Now, when you pass string_to_ascii( '240'), you get back string(18) "&#U050&#U052&#U048".

Sign up to request clarification or add additional context in comments.

Comments

0

Just found this:

function to_ascii($string) {
    $ascii_string = '';
    foreach (str_split($string) as $char) {
        $ascii_string .= '&#' . ord($char) . ';';
    }
    return $ascii_string;
}

here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.