homepage Welcome to WebmasterWorld Guest from 54.226.213.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP reg expression question
blaketar




msg:3635477
 10:44 pm on Apr 25, 2008 (gmt 0)

Simple question, what is the most efficient way to search for whole words in a string?

For example if I have two strings:

1.) This is the way to the lake.
2.) It was always like this.

When I search for the word "is" only the 1st string should match. My current script returns the second string also because it has "this" or i and s in the sequence.

 

cameraman




msg:3635481
 11:11 pm on Apr 25, 2008 (gmt 0)

I wouldn't be surprised if there were a better way, but this looks to be working:
#(^is)¦(\sis\s)¦(\sis$)#i

coopster




msg:3635774
 4:26 pm on Apr 26, 2008 (gmt 0)

You could use a word boundary [php.net].

g1smd




msg:3635869
 7:41 pm on Apr 26, 2008 (gmt 0)

This would work:

((^is(\ \,\.))(\ is(\ \,\.)))

- Begins with the word and is then followed by a space, comma or period.
- Has a space before, and a space, or comma, or period directly after.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved