homepage Welcome to WebmasterWorld Guest from 54.198.224.121
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Reading text file into an array (PHP)
is this the best method
ukgimp




msg:1300995
 1:44 pm on Jan 8, 2003 (gmt 0)

I wish to strip out a load of words from a query if they are present. I am having trouble getting the text file into and array so that I can use a str_replace.

The following works fine
$keywords = str_replace(array('x', 'y', 'z'), "", $keywords);

But I want to read the array in from a file. I cant seem to get it or find an example. I can get the file to echo to produce

x y z

Is there a simple way of doing this or do I have to print each time with 'array value', and then remove the trailing comma and then bung that variable into the above str_replace.

I have used the following to print to screen ok:

$filename = 'stopwords.txt';
$fp = fopen($filename, "r");
$stopwords = fread($fp, filesize($filename));
fclose($fp);
echo $stopwords;

Is the method I am trying to get to efficient or do I need to lok at another method.

TIA

 

andreasfriedrich




msg:1300996
 2:41 pm on Jan 8, 2003 (gmt 0)

You might consider building the array once and then serialize [php.net] and store it in a file. Then when you need the array open the file and unserialize the array.

The file [php.net] function reads an entire file into an array. Unfortunately it includes the end of line character as well which you would have to remove for each line.

$fp = @fopen($filename, 'r'); 
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}

The code snippet above gives you an array without the end of line character.

Andreas

ukgimp




msg:1300997
 3:45 pm on Jan 8, 2003 (gmt 0)

Thanks for your post Andreas.

My abilities with php fall somewhat short and as a result I have trouble with the absolute basics at times. Take this case for example, cant get the swine to work......

The @ seems strange, but what do I know :)

So I am off to create a function with a big long list of words and get use a find and replace to put the ''s in. :(

$keywords = str_replace(array('x', 'y', ..... 'n', "", $keywords);

Cheers

andreasfriedrich




msg:1300998
 4:28 pm on Jan 8, 2003 (gmt 0)

The @ sign disables error reporting.

So I am off to create a function with a big long list of words and get use a find and replace to put the ''s in.

Im not quite sure that I get your point on this one.

Given a stop.txt file like this

aaron 
nick

a string of keywords like this

tampa WebmasterWorld aaron sew sheffield

and a script like this

$filename = 'stop.txt'; 
$fp = @fopen($filename, 'r');
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}
$keywords = str_replace($array, "", $keywords);
echo $keywords;

will give you

tampa WebmasterWorld sew sheffield

Andreas

ukgimp




msg:1300999
 4:40 pm on Jan 8, 2003 (gmt 0)

Im not quite sure that I get your point on this one.

:) Sorry, that was me feeling strained after having difficulty getting something that should be simple to work and still missing the easy life of Christmas. I can get the word removal to work if I manually type in the array rather than import a text file which I could then create a function to do it instead. It just seemed like a clunky way of doing it.

I am still going to try and get to the bottom of it and thank you for your examples which should see me right. I like to be able to understand and do something myself even if it drives me up the wall. "It" has challenged me and I aint gonna give in.

Cheers

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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved