string length regular expression

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

string length regular expression

Post by kendall »

Hi guys,

can you use preg_match() to test for a string length instead of using the strlength()
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Yes, but str_len would be alot faster.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Zoxive wrote:Yes, but str_len would be alot faster.
was hoping to get a pattern example...i knew that already... :?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

kendall wrote:
Zoxive wrote:Yes, but str_len would be alot faster.
was hoping to get a pattern example...i knew that already... :?
I only answered your question as asked.

"Word Character" up to 10 characters long.

Code: Select all

^[\w]{0,10}$
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Code: Select all

// for educational purposes only 
preg_match('/^.{5}$/sD', 'abcde');
Since a string can contain any character, not just word characters, use the dot metacharacter. Also apply the s modifier to make the dot match newlines as well.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

GeertDD wrote:

Code: Select all

// for educational purposes only 
preg_match('/^.{5}$/sD', 'abcde');
Since a string can contain any character, not just word characters, use the dot metacharacter. Also apply the s modifier to make the dot match newlines as well.
hmmmm....neat!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

This has just been bugging me... and it's not an answer to the original post... but maybe it's because I'm a stickler. :-P

The function is strlen(). Not str_len() or strlength().

:D
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

hahahah....duly noted scottayy dude
Post Reply