can someone tell me whats wrong with my code?

$file= "/file.txt";
$search ='/$_GET[word]/i';
echo $search;
if (preg_match($search, $file))
{
echo "A match was found.";
}

Recommended Answers

All 5 Replies

You are using single quotes (') for your $search string. Variables aren't parsed inside single quotes. So the search string right now is literally '/$_GET[word]/i'

yes.. try:

$search = $_GET['word'];

yes.. try:

$search = $_GET['word'];

It's a regular expression so the correction would be

$search = "/$_GET[word]/i";

thx but how come it only works for some letters?

It works for all letters, you just can't use a variable inside single quotes.

$myVar = 'Hello World';
$string = '/$myVar/'; //== /$myVar/
$string = "/$myVar/" //== /Hello World/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.