string substiution char 192-255

Discussion in 'Perl Misc' started by John, Dec 14, 2006.

  1. John

    John Guest

    Hi

    I have a string which may contain European accented characters - chr(192)
    to chr(255).

    I need to replace those characters with Å (or whatever the number is)
    so that it can be displayed in HTML.

    I can use substr($string,$i,1) and walk through it checking for every
    character in that range and build a new string.

    However, is there a cleaner solution using $x~=s///?

    I cannot see it.

    Regards
    John
     
    John, Dec 14, 2006
    #1
    1. Advertising

  2. John

    Mirco Wahab Guest

    John wrote:
    > I have a string which may contain European accented characters - chr(192)
    > to chr(255).
    >
    > I need to replace those characters with Å (or whatever the number is)
    > so that it can be displayed in HTML.
    >
    > I can use substr($string,$i,1) and walk through it checking for every
    > character in that range and build a new string.
    >
    > However, is there a cleaner solution using $x~=s///?


    naíve solution:

    my $text = '
    à Ý X ó Õ Ø € Ç È Ê É ß and à and ò þ and @ and ± and Ç ô õ Å, Ý';

    $text =~ s/([\xc0-\xff])/'&#'.ord($1).';'/eg;

    just replace every character in range (between 0xC0 and 0xFF)
    by a string contructed via /e in substitution.

    Regards

    Mirco
     
    Mirco Wahab, Dec 14, 2006
    #2
    1. Advertising

  3. John

    Ben Morrow Guest

    Quoth "John" <>:
    > Hi
    >
    > I have a string which may contain European accented characters - chr(192)
    > to chr(255).
    >
    > I need to replace those characters with Å (or whatever the number is)
    > so that it can be displayed in HTML.
    >
    > I can use substr($string,$i,1) and walk through it checking for every
    > character in that range and build a new string.
    >
    > However, is there a cleaner solution using $x~=s///?


    HTML::Entities (which will also do <, >, & and " for you).

    Ben

    --
    If you put all the prophets, | You'd have so much more reason
    Mystics and saints | Than ever was born
    In one room together, | Out of all of the conflicts of time.
    The Levellers, 'Believers'
     
    Ben Morrow, Dec 14, 2006
    #3
  4. John

    John Guest

    "Mirco Wahab" <> wrote in message
    news:elrqkr$a97$-halle.de...
    > John wrote:
    >> I have a string which may contain European accented characters -
    >> chr(192) to chr(255).
    >>
    >> I need to replace those characters with Å (or whatever the number
    >> is) so that it can be displayed in HTML.
    >>
    >> I can use substr($string,$i,1) and walk through it checking for every
    >> character in that range and build a new string.
    >>
    >> However, is there a cleaner solution using $x~=s///?

    >
    > naíve solution:
    >
    > my $text = '
    > à Ý X ó Õ Ø € Ç È Ê É ß and à and ò þ and @ and ± and Ç ô õ Å, Ý';
    >
    > $text =~ s/([\xc0-\xff])/'&#'.ord($1).';'/eg;
    >
    > just replace every character in range (between 0xC0 and 0xFF)
    > by a string contructed via /e in substitution.
    >
    > Regards
    >
    > Mirco
    >
    >


    Many thanks.

    I couldn't work out how to link the chr() and the ord().

    It's clear now.

    Much appreciated.

    Regards
    John
     
    John, Dec 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Girish
    Replies:
    0
    Views:
    488
    Girish
    Jan 23, 2004
  2. Old and In the Way
    Replies:
    6
    Views:
    4,711
    Andrew Thompson
    Jul 22, 2006
  3. Replies:
    5
    Views:
    495
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,096
    Ian Collins
    May 9, 2006
  5. Replies:
    0
    Views:
    128
Loading...

Share This Page