Leandro Garcia
Leandro Garcia

Reputation: 3228

What does this regex achieve?

I've seen this...

preg_match("/.*" . $row['keyword'] . ".*/", $word, $matches);

What does the pattern try to imply?

Upvotes: 1

Views: 72

Answers (4)

Dr.Kameleon
Dr.Kameleon

Reputation: 22810

Explanation :

/                            # start of the regex
.                            # match anything (any character, etc - except for /n)
*                            # zero or more times
" . $row['keyword']. "       # match the keyword
.*                           # same as above
/                            # end of the regex

Upvotes: 3

Andreas Wong
Andreas Wong

Reputation: 60516

It's trying to find out whether $row['keyword'] is contained within $word, it's also safer to call preg_quote to $row['keyword'] in case the keyword contains meta characters such as *, /, \ etc

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359786

/ is just the pattern delimiter, and .* means "any character (other than newline) repeated 0 or more times," so it's simply searching for occurrences of whatever string is in $row['keyword'], in $word.

Upvotes: 1

alex
alex

Reputation: 490183

It means find the term followed or preceded by 0 or more characters (*) which are not \n (.).

Unless it is done elsewhere, you ought to wrap $row['keyword'] with preg_quote($row['keyword'], '/').

Upvotes: 3

Related Questions