SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regex allow alphanumeric + spaces

    hi all

    just after a little adjustment to this regex if possible
    need it to allow a-z, 0-9 and spaces

    PHP Code:
      $valid true;
      if (
    preg_match('/[\W]/i'$value$valid false
    accepts alphanumeric and underscores but returns false when spaces are in

    is there a small adjustment that can be made to fix this?
    sorry my regex skills are pretty pathetic

    thanks
    dave

  2. #2
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try...
    PHP Code:
    preg_match("/\w|\s+/"$value
    I did a search on Google and grabbed the answer from here...
    http://forums.devshed.com/t57184/s.h...any+characters

  3. #3
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    \s means whitespace characters

  4. #4
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's a whitespace character?

  5. #5
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a space or newline

  6. #6
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gavwvin
    a space or newline
    or a tab

  7. #7
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    k thanks guys, i'll give it a go
    whitespace is cool, i'll only need it for text fields anyway

    also, when someone says searched on google
    is it possible to say what your search term was
    i know that's asking a lot
    but sometimes the right phrase never comes to mind

  8. #8
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dtra
    also, when someone says searched on google
    is it possible to say what your search term was
    i know that's asking a lot
    but sometimes the right phrase never comes to mind
    There is a query string on the end of that link in post #2 with the search terms used ("many characters"). But you're right, it helps if people let you know what search terms they used. I don't always know what terms to search on myself.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  9. #9
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nah, that didn't work
    it's sort of backwards of what i'm trying to do
    i am checking to see if there are non-alphanumeric characters except for white space
    is there a way to do exceptions in perl regex?
    i tried the suggestion
    but even with [a-z\s], [\w\s], \w|\s, \w|\s+, nothing
    they all still crap on spaces, could i have a setting that's not set up or something?
    i'm running php 5 (installed from binary) on iis

  10. #10
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you do preg_match on $_SERVER['HTTP_REFERER'] to see whether its from google, then get the q= part, thats the search terms, you could then mail it to yourself ot add to a database. Heres the script that I use (in the head of a every page)

    PHP Code:
    function referer()
    {
        
    $parts=parse_url($_SERVER['HTTP_REFERER']);
        if(
    strstr($parts['host'],"google")) { //if they've come from google
            
    $get=preg_split("#&#",$parts['query']); //get query string of url
            
    foreach($get as $key=>$stuff) { //put into an array of var=value
                
    $data=preg_split("#=#",$stuff);
                
    $get[$data[0]]=$data[1];
                unset(
    $get[$key]);
            }
            if(
    $get['q']) { //if q= was in the query string
                
    $search_phrase=$get['q']; //thats the search terms
                
    if($get['start']) { //if start was set, that indicates what page they ame from
                    
    $page=($get['start']/10)+1;
                } else {
                    
    $page=1//..otherwise it was page 1
                
    }
                
    $entry_page=$_SERVER['PHP_SELF']; //mark what page they came in on
                
    $query="INSERT INTO google_terms VALUES('".$search_phrase."', '".$page."', '".$entry_page."')";
                @
    mysql_query($query); //put into database
            
    }
        }


  11. #11
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what was this aimed at?

    Quote Originally Posted by gavwvin
    if you do preg_match on $_SERVER['HTTP_REFERER'] to see whether its from google, then get the q= part, thats the search terms, you could then mail it to yourself ot add to a database. Heres the script that I use (in the head of a every page)

    PHP Code:
     function referer()
     {
         
    $parts=parse_url($_SERVER['HTTP_REFERER']);
         if(
    strstr($parts['host'],"google")) { //if they've come from google
             
    $get=preg_split("#&#",$parts['query']); //get query string of url
             
    foreach($get as $key=>$stuff) { //put into an array of var=value
                 
    $data=preg_split("#=#",$stuff);
                 
    $get[$data[0]]=$data[1];
                 unset(
    $get[$key]);
             }
             if(
    $get['q']) { //if q= was in the query string
                 
    $search_phrase=$get['q']; //thats the search terms
                 
    if($get['start']) { //if start was set, that indicates what page they ame from
                     
    $page=($get['start']/10)+1;
                 } else {
                     
    $page=1//..otherwise it was page 1
                 
    }
                 
    $entry_page=$_SERVER['PHP_SELF']; //mark what page they came in on
                 
    $query="INSERT INTO google_terms VALUES('".$search_phrase."', '".$page."', '".$entry_page."')";
                 @
    mysql_query($query); //put into database
             
    }
         }
     } 

  12. #12
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry I think I misunderstood
    also, when someone says searched on google
    is it possible to say what your search term was?
    I thought he meant how to tell what people searched for to get to your site, reading it again I see what he means

  13. #13
    SitePoint Evangelist
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    463
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    he, it was me, nm
    yeah, no i would never change the subject in my own thread (just want answers )
    and prolly not in someone else's


Bookmarks

Posting Permissions

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