Quick String Query...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jamie Wright

    Quick String Query...

    I have been using PHP for a while now, although my knowledge of the inbuilt
    functions is pretty lacking. I am trying to remove a 'space' from the middle
    of a string. I reckon I could split the string at teh space, trim each
    string then join them, but this seems a little long winded. Is there a
    command to remove certain charachters? Or even better white space? I've
    searched the PHP manual with little luck.

    Any help would be appreciated.

    Jamie


  • Jason Dumler

    #2
    Re: Quick String Query...

    Jamie,

    Try the "str_replac e" or "preg_repla ce" functions.

    str_replace is a straight forward string replacement function, while
    preg_replace lets you use perl regular expressions to replace strings
    inside other strings.

    Jason

    Jamie Wright wrote:[color=blue]
    > I have been using PHP for a while now, although my knowledge of the inbuilt
    > functions is pretty lacking. I am trying to remove a 'space' from the middle
    > of a string. I reckon I could split the string at teh space, trim each
    > string then join them, but this seems a little long winded. Is there a
    > command to remove certain charachters? Or even better white space? I've
    > searched the PHP manual with little luck.
    >
    > Any help would be appreciated.
    >
    > Jamie
    >
    >[/color]

    Comment

    • James Sleeman

      #3
      Re: Quick String Query...

      Jamie Wright wrote:
      [color=blue]
      > I have been using PHP for a while now, although my knowledge of the
      > inbuilt functions is pretty lacking. I am trying to remove a 'space' from
      > the middle of a string. I reckon I could split the string at teh space,[/color]

      $string = preg_replace('/\s+/', '', $string);

      --
      James Sleeman
      Gogo:Code http://www.gogo.co.nz/
      Email domain : gogo.co.nz see user in from header!

      Comment

      • James Sleeman

        #4
        Re: Quick String Query...

        Jamie Wright wrote:
        [color=blue][color=green]
        >> $string = preg_replace('/\s+/', '', $string);[/color]
        >
        > Cheers for that. You wouldn't care to explain the /\s+/ would you? Does
        > this mean 'space'?[/color]

        Yes, that is a regular expression, \s means whitespace and + means one or
        more, so the expression matches one or more whitespace characters.

        Regular Expression Syntax :
        PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


        --
        James Sleeman
        Gogo:Code http://www.gogo.co.nz/
        Email domain : gogo.co.nz see user in from header!

        Comment

        Working...