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. #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    15,060
    Thanks
    165
    Thanked 2,250 Times in 2,237 Posts
    PHP Code:
    if (preg_match("/\s/",trim($string))) {
        echo 
    $error "name cannot have embedded blanks!";

    ?

  3. #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. #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    3,348
    Thanks
    2
    Thanked 233 Times in 226 Posts
    The Perl solution would be:
    Code:
    if ( $string =~ /\s/ ) {
        $error = "name cannot have embedded blanks!";
    }

  5. #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    15,060
    Thanks
    165
    Thanked 2,250 Times in 2,237 Posts
    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

  6. #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
  •