Welcome to WebmasterWorld Guest from 54.144.107.83

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP reg expression question

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

Full Member

10+ Year Member

joined:June 24, 2004
posts: 202
votes: 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.

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

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


I wouldn't be surprised if there were a better way, but this looks to be working:
#(^is)¦(\sis\s)¦(\sis$)#i
4:26 pm on Apr 26, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


You could use a word boundary [php.net].
7:41 pm on Apr 26, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members