bobleny Posted June 24, 2007 Share Posted June 24, 2007 This may sound stupid, but how do get preg_match to look for spaces as well? This is what I have: preg_match("/[^a-zA-Z0-9_]+/", $str) if $str equals "you_foo", preg_match proves false, thats good. if $str equals "you foo", preg_match proves true, thats bad. How do i get it to accept spaces? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/ Share on other sites More sharing options...
Wuhtzu Posted June 24, 2007 Share Posted June 24, 2007 I don't know if there is a prettier way to do it but a normal space (' ') works just fine... <?php $string = 'some string with spaces'; if(preg_match('/^[a-z0-9 _]+$/',$string)) { echo 'Match'; } else { echo 'No match'; } ?> The above results in 'Match' being displayed in my browser. Else take a look at the \s which matches whitespace - spaces, new lines, tabs ect. Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281490 Share on other sites More sharing options...
Caesar Posted June 24, 2007 Share Posted June 24, 2007 <?php $str = "you_foo"; preg_match('/[^a-zA-Z0-9_\s]+/', $str); //...."\s" = single space character. ?> Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281492 Share on other sites More sharing options...
Wuhtzu Posted June 24, 2007 Share Posted June 24, 2007 <?php $str = "you_foo"; preg_match('/[^a-zA-Z0-9_\s]+/', $str); //...."\s" = single space character. ?> Not true.... \s means any whitespace, new lines, spaces, multiple spaces and tabs. <?php $string = "some\nstring\nwith\nspaces"; if(preg_match('/^[a-z0-9\s]+$/',$string)) { echo 'Match'; } else { echo 'No match'; } ?> The above code results in 'Match' being displayed in the browser... Note that \ (backslash) isn't present as an allowed character in my regex but \s is and it allows \n (new line)... Btw this is a good resource for regex stuff: http://www.regular-expressions.info/reference.html Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281495 Share on other sites More sharing options...
bobleny Posted June 24, 2007 Author Share Posted June 24, 2007 Well, neither of them worked... Here is what I have: if (preg_match("/[^a-zA-Z0-9_]+/", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } basically, when I enter, "you_foo": $_POST['key'] = "you_foo"; If I enter "you foo": $_POST['key'] = "sfasd3254djs45dkjd"; Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281496 Share on other sites More sharing options...
Wuhtzu Posted June 24, 2007 Share Posted June 24, 2007 Well you didn't add any space (' ') to your regex... Yours: "/[^a-zA-Z0-9_]+/" With space: "[^a-zA-Z0-9 _]+/" ...... Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281497 Share on other sites More sharing options...
Wuhtzu Posted June 24, 2007 Share Posted June 24, 2007 Another thing to be aware of, is the fact that you only require the string to start with your regex, not end with it: preg_match('/^[a-z]+/', 'some string 04595867&%¤#"!') will return true because the start of the string is within the range of a-z preg_match('/^[a-z]+$/', 'some string 04595867&%¤#"!') will return false because it tries to match all the way to the end of the string which holds chars outside the a-z range.. Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281501 Share on other sites More sharing options...
bobleny Posted June 24, 2007 Author Share Posted June 24, 2007 Well, here, try it. http://www.firemelt.net/crow/index.php?page=base type in "you_foo" then type in "you foo" if it fails the preg match it will say "No Match!" if it passes the preg match, it will display what you entered... this is the current preg_match: if (preg_match("/[^a-zA-Z0-9 _]+/", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } Edit: Here is the if statement that coincides with the preg_match.... if ($_POST['key'] == "sfasd3254djs45dkjd") { $_POST['key'] = "No Match!"; } Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281516 Share on other sites More sharing options...
wildteen88 Posted June 24, 2007 Share Posted June 24, 2007 Read Wuhtzu post above, I quote it here read the bit in red: Another thing to be aware of, is the fact that you only require the string to start with your regex, not end with it: preg_match('/^[a-z]+/', 'some string 04595867&%¤#"!') will return true because the start of the string is within the range of a-z preg_match('/^[a-z]+$/', 'some string 04595867&%¤#"!') will return false because it tries to match all the way to the end of the string which holds chars outside the a-z range.. Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281525 Share on other sites More sharing options...
bobleny Posted June 24, 2007 Author Share Posted June 24, 2007 I did read his post and I don't understand, look: $_POST['key'] = "some string 04595867&%¤#!" if (preg_match("/[^a-zA-Z0-9 _]+/", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } $_POST['key'] always returns as "sfasd3254djs45dkjd". Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281528 Share on other sites More sharing options...
bobleny Posted June 24, 2007 Author Share Posted June 24, 2007 Here is the code to the page: <?php echo "<div class='text_body_box_declare'>Search</div>"; echo "<div class='text_body_box'>"; echo "<div class='text_body_box_text'>"; echo "Welcome to the Knowledge Database! Use this as a tool to help you with your studies. Simply enter a keyword to view its definition and click \"Search\"."; echo "<hr class='short'>"; echo "<form name='form' action='index.php?page=base' method='post'>"; echo "Keyword: <div class='login_input'><input type='text' name='key' size='30' /></div>"; echo "<div class='login_input'><input type='submit' name='search' value='Search' /></div>"; echo "</form>"; echo "</div>"; echo "</div>"; if ($_POST['search'] == TRUE) { if (preg_match("/[^a-zA-Z0-9 _]+/", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } connect($page, __LINE__); query($page, __LINE__, "SELECT * FROM `base` WHERE `keywords` = '" . $_POST['key'] . "'"); mysql_close(); $fetch = mysql_fetch_assoc($query); if (!$fetch) { if ($_POST['key'] == "sfasd3254djs45dkjd") { $_POST['key'] = "No Match!"; } echo "<div class='text_body_box_declare'>" . $_POST['key'] . "</div>"; echo "<div class='text_body_box'>"; echo "<div class='text_body_box_text'>"; echo "Your search turned up no results!"; if ($_SESSION['user_logged_code_1634'] == TRUE && $_SESSION['username'] === "Admin") { echo "<div class='admin_text_edit'>"; echo "<div class='float_right'>"; echo "<form action='index.php?page=base&admin=edit' method='post'>"; echo "<div class='login_input'><input type='submit' name='edit' value='Edit' /></div>"; echo "</form>"; echo "</div>"; echo "</div>"; } echo "</div>"; echo "</div>"; } else { echo "<div class='text_body_box_declare'>" . $_POST['key'] . "</div>"; echo "<div class='text_body_box'>"; echo "<div class='text_body_box_text'>"; echo $fetch['definition']; if ($_SESSION['user_logged_code_1634'] == TRUE && $_SESSION['username'] === "Admin") { echo "<div class='admin_text_edit'>"; echo "<div class='float_right'>"; echo "<form action='index.php?page=base&admin=edit' method='post'>"; echo "<div class='login_input'><input type='submit' name='edit' value='Edit' /></div>"; echo "</form>"; echo "</div>"; echo "</div>"; } echo "</div>"; echo "</div>"; } } ?> Go to: http://www.firemelt.net/crow/index.php?page=base Enter in this line: some string 04595867&%¤#"! You'll see that it doesn't pass the preg_match... Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281530 Share on other sites More sharing options...
sasa Posted June 25, 2007 Share Posted June 25, 2007 "/[^a-zA-Z0-9 _]+/" means one or more characters that is NOT in sets a-z or A-Z or ' ' or '_' the string 04595867&%¤#"! have character that is not in set (& or % etc.) and preg_match is True, true or anything = true $_POST['key'] = 'sfasd3254djs45dkjd' and echo is no match Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281839 Share on other sites More sharing options...
redarrow Posted June 25, 2007 Share Posted June 25, 2007 <?php $word="i am redarrow"; if(preg_match("/[^a-zA-Z][][a-zA-Z][][a-zA-Z]/", $word)){ echo " there are spaces"; }else{ echo "there are no spaces"; } ?> [ ] to match a single space. Use whichever you find more readable. Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281860 Share on other sites More sharing options...
sasa Posted June 25, 2007 Share Posted June 25, 2007 redarrow, are you try this code try <?php $word ="s a s a"; //$word = "sasa+test"; // 2nd test if(preg_match("/[^a-zA-Z][][a-zA-Z][][a-zA-Z]/", $word, $s)){ echo " there are spaces"; }else{ echo "there are no spaces"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281876 Share on other sites More sharing options...
redarrow Posted June 25, 2007 Share Posted June 25, 2007 ok cheers but that weried according to the manual from regbuddy that correct a white space can be used with [] or \ so how to detect then a white space or space. according to the manual all spaces are seen as no spaces so how do you do it correctly. Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281887 Share on other sites More sharing options...
sasa Posted June 25, 2007 Share Posted June 25, 2007 just <?php $word ="s a s a"; //$word = "sasa+test"; // 2nd test if(preg_match("/ /", $word)){ echo "there are spaces"; }else{ echo "there are no spaces"; } ?> but the problem is different in string the regular characters is A-Z and a-z and 0-9 and ' ' (space) and '_' if string have any other character(s) then it is not regular <?php $word = 'sasa & angelina'; if (preg_match("/[^a-zA-Z0-9 _]+/", $word)) echo 'not regular'; else echo 'regular'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281894 Share on other sites More sharing options...
redarrow Posted June 25, 2007 Share Posted June 25, 2007 Thank you so in essance there is no way really except a reel space in the [condition ] or this / / Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-281896 Share on other sites More sharing options...
rea|and Posted June 25, 2007 Share Posted June 25, 2007 Another way to write the characters is \xXX syntax, where XX is the hexadecimal value of a char. So : $rex='/\x20/'; # white space $rex='/\x0A/'; # new line # etc that I guess it is more readable. Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-282057 Share on other sites More sharing options...
wildteen88 Posted June 25, 2007 Share Posted June 25, 2007 I did read his post and I don't understand, look: $_POST['key'] = "some string 04595867&%¤#!" if (preg_match("/[^a-zA-Z0-9 _]+/", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } $_POST['key'] always returns as "sfasd3254djs45dkjd". Your regex is incorrect. Your regex is not searching the whole of the string. Currently it only goes as far as the first space - by default regex stops at whitespace (spaces, newlines etc). What you need to tell PHP to do is search the whole entire string from the beginning to the end. You do this by placing the carrot character (^) at the start of the pattern (not within a character class ([]) and then place the dollar sign ($) at the very end of the pattern. Then within the character class you place the characters you wish to allow so letters a-z and digits 0-9 So if you use the the following pattern you code should work as expected: /^[a-zA-Z0-9_]+/$ Your code with new pattern: <?php $_POST['key'] = "some 0255"; if (preg_match("/^[a-zA-Z0-9_]+$/", $_POST['key']) || strlen($_POST['key']) < 2) { echo 'Contains valid characters'; } else { echo 'Contains invalid characters'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-282237 Share on other sites More sharing options...
bobleny Posted June 28, 2007 Author Share Posted June 28, 2007 OK fine, I changed it: if (preg_match("/^[a-zA-Z0-9_]+/$", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } It works exactly the same as the last, and I still cant get it to pass the " " (space).... I've tried: if (preg_match("/^[a-zA-Z0-9 _]+/$", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } if (preg_match("/^[a-zA-Z0-9/\x20/_]+/$", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } if (preg_match("/^[a-zA-Z0-9/x20/_]+/$", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } if (preg_match("/^[a-zA-Z0-9x20_]+/$", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } if (preg_match("/^[a-zA-Z0-9/ /_]+/$", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } --------------------------------------- OK, what do I do now? :'( Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-284579 Share on other sites More sharing options...
sasa Posted June 28, 2007 Share Posted June 28, 2007 try if (preg_match("/[^a-zA-Z0-9 _]+/", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } or if (!preg_match("/^[a-zA-Z0-9 _]+$/", $_POST['key']) || strlen($_POST['key']) < 2) { $_POST['key'] = "sfasd3254djs45dkjd"; } they are same Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-284605 Share on other sites More sharing options...
Wuhtzu Posted June 28, 2007 Share Posted June 28, 2007 How about getting a regex to accept spaces before worrying about $_POST['key'] and the length of that... Regex which accepts letters, numbers, spaces and underscores: <?php $regex = "/^[a-z0-9 _]+$/i"; $string = "String with NUMers 1337 and underscores _ _ _ __"; if(preg_match($regex,$string)){ echo "The string only contains _valid_ characters"; } else{ echo "The string cotnains _invalid_ characters"; } ?> Can you get the above script to echo "The string only contains _valid_ characters" ? Quote Link to comment https://forums.phpfreaks.com/topic/56985-spaces-in-a-preg_match/#findComment-284727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.