Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Embedded spaces in string

    How do I determine if a string has embedded spaces? The following does not work. It has to ignore trailing spaces, but not leading spaces. Is there an easy way to do that?

    This string has embedded spaces.

    Thisstringdoesnothaveembeddedspaces.

    if ($string =~ m/[^a-zA-Z0-9]/) {
    $error = "name cannot have embedded blanks!";
    }

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    if (preg_match("/\s/",trim($string))) {
        echo 
    $error "name cannot have embedded blanks!";

    ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I get an error message with that...

    Undefined subroutine &main::trim called at text_file_update.cgi line 41.

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,869
    Thanks
    2
    Thanked 163 Times in 158 Posts
    The Perl solution would be:
    Code:
    if ( $string =~ /\s/ ) {
        $error = "name cannot have embedded blanks!";
    }

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by tdavis View Post
    I get an error message with that...

    Undefined subroutine &main::trim called at text_file_update.cgi line 41.
    Sorry, I thought it's in PHP forum
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks fishmonger. That is what I needed.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •