470,641 Members | 1,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

How can I improve this regex ?

290 100+

I am not sure if this can be solved with regex,
possibly the string needs to be chopped into words
and then stepped through ( but not sure how).

Anyway, this is what I have and it is very close to what
I want.:
Expand|Select|Wrap|Line Numbers
  1. preg_match_all("#((?:\b\w{1,20}\b\s+){2})#", $data, $matches) 
Here is part of the out put from print_r($matches);

[32] => technical support [33] => services attempt [34] => to help
[35] => the user [36] => solve specific [37] => problems with

As you can see, the data is just being divided into two word chunks.

And I am missing half of the possible phrases eg "support services"
is not reported.

This is not quite what I expected

What I wanted was a list of all the two word phrases,
so I should be getting:

[32] => technical support [33] => support services [34] => services attempt
[35] => attempt to [36] => to help [37] => help the [38] => the user

You see the overlap ?
This ensures that I do get all the phrases.

Any ideas on how would I need to change my
regex to achieve my desired output ?

If not possible, how else can I achieve it ?
Nov 7 '09 #1
4 1362
8,656 Expert Mod 8TB
there’s nothing you can do while using preg_match_all()

After the first match is found, the subsequent searches are continued on from end of the last match.
Nov 7 '09 #2
290 100+
So I think the "b" part of the question comes into play.

Any suggestions ?
Nov 7 '09 #3
8,656 Expert Mod 8TB
\b = word boundary

otherwise see above quote
Nov 7 '09 #4
5,058 Expert 4TB

I don't see a way to do this using regexp alone. It just searches for patterns, it doesn't do logic.

You could just split the string into induvidual words and have PHP pair the together, two and two.
A loop that goes through each word in the array, partnering it up with the next word in the list, added to a second array.

Expand|Select|Wrap|Line Numbers
  1. $words = explode(' ', $input);
  2. for($i = 1; $i < count($words) - 1; ++$i) {
  3.   $pairs[] = $words[$i-1] . " " . $words[$i];
  4. }
Nov 7 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Tim Conner | last post: by
23 posts views Thread by philipl | last post: by
20 posts views Thread by jeevankodali | last post: by
17 posts views Thread by clintonG | last post: by
reply views Thread by hardieca | last post: by
6 posts views Thread by Extremest | last post: by
7 posts views Thread by Extremest | last post: by
3 posts views Thread by aspineux | last post: by
15 posts views Thread by morleyc | last post: by
1 post views Thread by Korara | last post: by
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.