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
https://forums.phpfreaks.com/topic/235387-how-do-i-match-the-spacebar-in-regex/
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.

Archived

This topic is now archived and is closed to further replies.

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