I’m trying to Search my orderid, This is my code so far:
<?php
$file = 'order.dat';
$searchfor = '177';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
now for example my order.dat file contains the data
175|RC456456456|54156456465|109$
176|RC456456456|54156456465|177$
177|RC456456456|54156456465|129$
178|RC456456456|54156456465|129$
now when i searchfor 177 i have an result like this:-
Found matches:
176|RC456456456|54156456465|177$
177|RC456456456|54156456465|129$
But i want to search 177 for my 1st string only
I want my result like this, its only match my 1st string not on all 4 variables
177|RC456456456|54156456465|129$
Looks like you want to match only those string that begin with
177. If yes you can change your regex to: