Jump to content

How do I match the spacebar in regex?


JeremyCanada26

Recommended Posts

I'm trying to regex match a search phrase that can be any letter or number and be either one word or more than one word, also between 2 and 100 in length.

 

function checkSearchPhrase()
{
	if(isset($_POST['sp']))
	{
		//matches any single word or more
		if(preg_match("/^[A-Za-z0-9]{1,100}$/", $_POST['sp']) == 1)
		{
			return trim($_POST['sp']);
		} else
		{
			return false;
		}
	} else
	{
		return false;
	}
}

Link to comment
Share on other sites

You can match a space character by typing a space character (unless the PCRE_EXTENDED pattern modifier is used), or by using a backslash escape sequence to represent that character by its ASCII value.  These are basic regexes that will match a space: / / and /\040/ (octal ASCII value, also /\40/ provided there are fewer than 40 previous capturing subpatterns) and /\x20/ (hexadecimal ASCII value).

 

In contrast, the \s will match more than just the space character; it matches Horizontal Tab, Line Feed, Form Feed, Carriage Return and Space.

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.