Schlo_50 Posted May 20, 2008 Share Posted May 20, 2008 Hey guys, I have a problem and am in need of some assistance. I want to make a search facility to search for people by their telephone number. I have had a go but am running into problems with the results. Using the code below what happens is, if I search for '07' to find all mobile numbers I actually get every number with the combination '07' within it. E.g: '07' might find 07795986251 and 01207456658, but i only want the first. Can somebody help? $lines = file("data/vacancies.DAT"); foreach ($lines as $line) { $data[$key] = explode("|", $line); $number = str_replace(' ', '', $data[$key][7]); $searchb = str_replace(' ', '', $_POST['c']); if (preg_match("/$searchb/i", $number)) { print "match.<br />"; } } Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/106421-search-problem/ Share on other sites More sharing options...
MadTechie Posted May 20, 2008 Share Posted May 20, 2008 change if (preg_match("/$searchb/i", $number)) { to if (preg_match("/^$searchb/i", $number)) { Link to comment https://forums.phpfreaks.com/topic/106421-search-problem/#findComment-545513 Share on other sites More sharing options...
Schlo_50 Posted May 20, 2008 Author Share Posted May 20, 2008 Thank-you so much. What does the ^ mean? Link to comment https://forums.phpfreaks.com/topic/106421-search-problem/#findComment-545517 Share on other sites More sharing options...
MadTechie Posted May 20, 2008 Share Posted May 20, 2008 in this case (being at the very start) ^ means starts with but don't get confused with [^123] which would mean exclude 1,2 & 3 Link to comment https://forums.phpfreaks.com/topic/106421-search-problem/#findComment-545520 Share on other sites More sharing options...
Schlo_50 Posted May 20, 2008 Author Share Posted May 20, 2008 If say I wanted to search both of the following: $number = str_replace(' ', '', $data[$key][9]); $numberb = str_replace(' ', '', $data[$key][11]); How would I incorporate that into preg_match? if (preg_match("/^$searchb/i", $number, $numberb)) { Is that correct? Link to comment https://forums.phpfreaks.com/topic/106421-search-problem/#findComment-545525 Share on other sites More sharing options...
MadTechie Posted May 20, 2008 Share Posted May 20, 2008 do you mean $number AND $numberb or $number OR $numberb an examples would help Link to comment https://forums.phpfreaks.com/topic/106421-search-problem/#findComment-545529 Share on other sites More sharing options...
Schlo_50 Posted May 20, 2008 Author Share Posted May 20, 2008 Number AND Numberb please. Link to comment https://forums.phpfreaks.com/topic/106421-search-problem/#findComment-545686 Share on other sites More sharing options...
MadTechie Posted May 20, 2008 Share Posted May 20, 2008 <?php $lines = file("data/vacancies.DAT"); foreach ($lines as $line) { $data[$key] = explode("|", $line); $number = str_replace(' ', '', $data[$key][7]); $numberb = str_replace(' ', '', $data[$key][11]); $searchb = str_replace(' ', '', $_POST['c']); //finds $number at the start and must contain $numberb anywhere if (preg_match("/^$searchb/i", $number) && preg_match("/$searchb/i", $numberb)) { print "match.<br />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/106421-search-problem/#findComment-545693 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.