tags:

views:

115

answers:

3

Hi,

I want to match the word/pattern that is contained in the variable, but only match against the words that don't have white spaces. Please give suggestions.

$var = 'look';

$array = ('look', 'greatlook', 'lookgreat', 'look great', 'badlook', 'look bad', 'look ', ' look');

matches words: look, greatlook, lookgreat, badlook

non matches: look great, bad look, look (trailing space(s)), (space(s)) look.

The syntax of the below functions are OK, but it matches everything

$match = preg_grep ("/$var/", $array);

$match = preg_grep ("/^$var/", $array); (match words with 'look' at the start)

but when I include the [^\s], it gives an error

$match = preg_grep ("/$var[^\s]/", $array);

Parse error: syntax error, unexpected '^', expecting T_STRING or T_VARIABLE

TIA

A: 

Use:

$match = preg_grep("/^\S*$var\S*$/", $array);
Matthew Flaschen
Thanks very much, this works.I also removed the \S* at the front of the expression to force matches only with 'look' as the starting characters. Can you explain what the \ preceding the $ is for? I don't searched but I don't see the \ as a requirement for the $ (End of a string).
Jamex
@Jamex, actually, it works either way here. I was worried the `$` would be interpreted as a PHP variable [sigil](http://en.wikipedia.org/wiki/Sigil_%28computer_programming%29).
Matthew Flaschen
@Matt, actually, your original solution worked for me, "/^\S*$var\S*$/". I just removed the starting part of the expression for another purpose. Sorry for the murky reply, it was late.
Jamex
A: 
$match = preg_grep ("/{$var}[^\s]/", $array);

I believe that you need to enclose the variable in curly brackets because the characters continue on without whitespace.

waiwai933
That means the next character isn't whitespace. "looka " still matches. Also, words ending in $var (including look) don't, because there is no next character.
Matthew Flaschen
Thanks, but this does not work, the curly brackets alter the pattern.
Jamex
+1  A: 

The regex would be ^(?=.*look)[^\s]+$

preg_match("/^(?=.*{$var})[^\\s]+$/", $str);

<?
  $str = array('look', 'greatlook', 'lookgreat', 'look great', 'badlook', 'look bad', 'look ', ' look');
  $var = "look";
  $matches = preg_grep("/^(?=.*{$var})[^\\s]+$/", $str);
  print_r ($matches);
?>
//output
Array
(
    [0] => look
    [1] => greatlook
    [2] => lookgreat
    [4] => badlook
)
Amarghosh
Thanks but this does not work, the extra \ in the [^\\s] is giving an error, even without the extra slash, the regex is still causing multiple errors, mainly due altering the structure of the $var's pattern.
Jamex
@jamex How about `"^(?=.*look)[^\\s]+$"`
Amarghosh
That does not work either, "preg_grep() [function.preg-grep]: Unknown modifier '\' in". Even if one of the \ is removed, it is still showing the same error.
Jamex
@Jamex oops, I forgot delimiters. See the update.
Amarghosh
@Amarghosh, Thanks for your effort, sorry that I did not catch the error. I am not familiar with the syntax, and it was late.
Jamex