Jump to content

spaces in a preg_match(?????)


bobleny

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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!";
}

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

"/[^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

Link to comment
Share on other sites

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";
}

?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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';
?>

Link to comment
Share on other sites

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';
}

?>

Link to comment
Share on other sites

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? :'(

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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" ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.