need help with RegEX

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Super Mango

    #1

    need help with RegEX

    Hi,

    I want to change a given string in a way that:
    1) all invisible chars (like ALT+0254) will be changed to spaces
    2) all sequence of several whitespaces will be changed to a single
    space.
    3) leading and trailing spaces will be canceled (I know how to do that
    with trim, so this is only nice-to-have)

    I cant limit the string to a-zA-Z0-9 because that every visible char is
    OK (it's a multi lingual site).

    Any idea?

    Thanks in advance!

  • Toby Inkster

    #2
    Re: need help with RegEX

    Super Mango wrote:
    [color=blue]
    > 1) all invisible chars (like ALT+0254) will be changed to spaces
    > 2) all sequence of several whitespaces will be changed to a single
    > space.
    > 3) leading and trailing spaces will be canceled (I know how to do that
    > with trim, so this is only nice-to-have)[/color]

    Unless you want to jump through hoops, you'll probably want not one, but
    two regular expressions here. The first expression takes care of your
    first two requests; the second does the trimming.

    // Replace multiple white-space character with a single space.
    $out = preg_replace('/\s+/', ' ', $in);
    // Trim leading and trailing space.
    $out = preg_replace('/(^ )|( $)/', '', $out);

    Now, "\s" in a preg_replace only matches the common whitespace characters
    (space, formfeed, newline, carriage return, horizontal tab, and vertical
    tab).

    Unicode does have plenty of other space-like characters, so you may want
    to explicity add them to the first expression, like:

    /[\s\x00A0\x200B\ xFEFF]+/

    and so on. A good list of Unicode space-like characters can be found here:



    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact

    Comment

    • Super Mango

      #3
      Re: need help with RegEX

      Thank you SO much!!

      Comment

      • Jim Michaels

        #4
        Re: need help with RegEX


        "Super Mango" <liebermann@gma il.com> wrote in message
        news:1137567117 .302565.44500@g 14g2000cwa.goog legroups.com...[color=blue]
        > Hi,
        >
        > I want to change a given string in a way that:
        > 1) all invisible chars (like ALT+0254) will be changed to spaces
        > 2) all sequence of several whitespaces will be changed to a single
        > space.
        > 3) leading and trailing spaces will be canceled (I know how to do that
        > with trim, so this is only nice-to-have)
        >
        > I cant limit the string to a-zA-Z0-9 because that every visible char is
        > OK (it's a multi lingual site).[/color]

        you have a problem. I don't see how a regex is going to work with UNICODE
        or UTF-8 data. it can only handle chars up to 255 (\xff).
        [color=blue]
        >
        > Any idea?
        >
        > Thanks in advance!
        >[/color]


        Comment

        • Jim Michaels

          #5
          Re: need help with RegEX


          "Jim Michaels" <jmichae3@nospa m.yahoo.com> wrote in message
          news:nZqdnTRe1_ XSvmjeRVn-jQ@comcast.com. ..[color=blue]
          >
          > "Super Mango" <liebermann@gma il.com> wrote in message
          > news:1137567117 .302565.44500@g 14g2000cwa.goog legroups.com...[color=green]
          >> Hi,
          >>
          >> I want to change a given string in a way that:
          >> 1) all invisible chars (like ALT+0254) will be changed to spaces
          >> 2) all sequence of several whitespaces will be changed to a single
          >> space.
          >> 3) leading and trailing spaces will be canceled (I know how to do that
          >> with trim, so this is only nice-to-have)
          >>
          >> I cant limit the string to a-zA-Z0-9 because that every visible char is
          >> OK (it's a multi lingual site).[/color]
          >
          > you have a problem. I don't see how a regex is going to work with UNICODE
          > or UTF-8 data. it can only handle chars up to 255 (\xff).[/color]

          correction. it does handle them \x{fe23}. but now you have to know where
          the character sets are mapped in the font(s) and the ranges.
          <?php print preg_match("/\x{ffff}/","\xff"); ?>
          ^Z
          0

          [color=blue]
          >[color=green]
          >>
          >> Any idea?
          >>
          >> Thanks in advance!
          >>[/color]
          >
          >[/color]


          Comment

          Working...