homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Finding an exact match
not working

Msg#: 4458261 posted 12:03 am on May 27, 2012 (gmt 0)

Need to find an exact match from within a .txt or .htm file and there could be an empty space before any given number.

The code I tested below finds it, but not an exact match; what am I doing wrong?


$filename = 'if3.txt';

//$searchfor = '/^123456$/';


// $searchfor = "/\b123456/\b";

// $searchfor = '/\b123456\b/i';

$searchfor = '/^123/';

$file = file_get_contents($filename);
// if(strstr("\b$file\b", $searchfor))

if(stristr($file, $searchfor))

//if(preg_match("\b$file\b", "$searchfor"))

echo "String found";

echo "Nope";





WebmasterWorld Administrator incredibill us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Msg#: 4458261 posted 1:42 am on May 27, 2012 (gmt 0)

you can't mix and match search patterns with the wrong search function as stristr() and preg_match() don't do the same thing, one uses regular expressions, the other does not,


Msg#: 4458261 posted 2:11 am on May 27, 2012 (gmt 0)

I understand. My fault, the mix and match in my example were commented and I should not have posted my entire "test" script.

The idea behind this, that I want to create a login page where, if a "specific" pre-determined registration number is given/sent to the user, the visits the login page and the number(s) exists in the file, to execute / login rights allowed.

For example, if a user's registration number is 12345 while another is 123, would like to first search to see if the user's number exists, then do something().

Therefore, searching for "12345" would not be the same if "123" exists already as would be the same for "234", so it has to find a specific number or numbers.

1) The person visits the initial page
2) The person enters the number code assigned
3) When clicking on "submit", checks to see if number exists in file
4) If exact number match exists, proceed to the page.
5) If match is not found, show a message.

If it makes a difference, there stands to be a "space" before and/or after the number(s).

This would be in an existing file and probably not in any specific order.



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

Msg#: 4458261 posted 11:19 pm on Jun 1, 2012 (gmt 0)

Just trim() the number of any whitespace.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved