Encoding a string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • howachen@gmail.com

    Encoding a string?

    Suppose I have a string,

    e.g. $str = 'good morning';


    I want encode the string into a format that doesn't contains any
    special character like \n, \r and space etc.


    Currently base64_encode is used. But are there any faster methods?


    Thanks.

    Howa

  • samudasu

    #2
    Re: Encoding a string?

    use trim on the string to remove \n \r \t spaces, etc.
    $str = 'good morning';
    $trimmed = trim($str);

    Comment

    • Ewoud Dronkert

      #3
      Re: Encoding a string?

      howachen@gmail. com wrote:[color=blue]
      > I want encode the string into a format that doesn't contains any
      > special character like \n, \r and space etc.
      >
      > Currently base64_encode is used. But are there any faster methods?[/color]

      Try urlencode, addslashes, htmlspecialchar s, htmlentities, str_replace,
      preg_replace, strtr.

      --
      E. Dronkert

      Comment

      • Sjoerd

        #4
        Re: Encoding a string?

        howachen@gmail. com wrote:[color=blue]
        > Currently base64_encode is used. But are there any faster methods?[/color]

        No. These are some functions used to encode 11 MB of data and the time
        they took to do so:

        base64_encode: 0.105 sec
        urlencode: 0.122 sec
        addslashes: 0.114 sec
        htmlspecialchar s: 0.400 sec
        htmlentities: 1.145 sec

        Comment

        • Jasen Betts

          #5
          Re: Encoding a string?

          On 2006-02-19, howachen@gmail. com <howachen@gmail .com> wrote:[color=blue]
          > Suppose I have a string,
          >
          > e.g. $str = 'good morning';
          >
          >
          > I want encode the string into a format that doesn't contains any
          > special character like \n, \r and space etc.[/color]

          addslashes



          --

          Bye.
          Jasen

          Comment

          • howachen@gmail.com

            #6
            Re: Encoding a string?

            oh....great! seems base64 is the fastest one.

            Thanks for your information!

            Rdgs,
            howa

            Comment

            Working...