Jump to content

Reading file for certain strings


Schlo_50

Recommended Posts

Hi there, I am trying to create an advanced search feature where by a person can search through a list of job vacancies to find suitable jobs for candidates. The last stage of my script is meant to search through a .DAT file for keywords stored in my array. I can open the file using fopen() but im not sure what I need to do to find my key words and then print out the full file contents. Im assuming preg_match() but im useless with this function. Could somebody help? My code is below:

 

if ($salary >= $from && $salary <= $to){

// If there are vacancies matching the wage band grab id and start searching descriptions for skill matches

	$handle = fopen("data/$vacancy_id.DAT", "r");
// split skills up in array
	$search = $_POST['b'];
	$skill = (str_word_count($search, 1, 'àáãç'));

	foreach ($skill as $var){
// try to match
	preg_match_all($var, $handle)
// print out matches
		print "Found Match!<br />";

		}

}

 

ANybody got any ideas?

 

Much appreciated! Thanks

 

Link to comment
Share on other sites

fopen() just creates a file system pointer resource, you need to use it in conjunction with fread() to get the contents of the file. Though, for this purpose, it'd probably be better to use file_get_contents().

 

If you are only matching strings, and not patterns, RegEx functions are probably a waste of resources, you might be better off using something like strpos().

 

<?php
$contents = file_get_contents("foo.dat");
$skill = array("lorem","ipsum","dolar","sit","amet");
$found = false;

foreach($skill as $s) {
    if(strpos($content,$s)) {
        $found = true;
        break;
    }
}

echo ($found) ? "Matches found in:<br />".$content : "No matches.";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.