natasha_thomas Posted October 23, 2010 Share Posted October 23, 2010 Friends, Requirement: I want to Extract the Keywords from All the Search Engine Referer URLs and save all the keywords in an Array so i can Echo/show all the "popular Terms" which drive traffic to my Site from Search Engines. I already have the Code for Extracting the Keyword from URL, can someone tell me how can i save the $keyword in an array that whenever i visit my site, i can see that What all keywords brought visitors to my site from Search Engines? Code to Extract Keywords from URL: <? $referrer = $_SERVER['HTTP_REFERER']; $referrer ='http://www.google.co.id/search?hl=id&client=firefox-a&hs=IKn&rls=org.mozilla%3Aen-US%3Aofficial&channel=s&q=pemahaman+pelajaran+ips+tentang+endogen+dan+eksogen&aq=f&aqi=&aql=&oq=&gs_rfai='; $referrer_query = parse_url($referrer); $referrer_host = $referrer_query['host']; $referrer_query = $referrer_query['query']; $q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword); $keyword = urldecode($keyword[1]); $potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } if ($keyword=="") { $potongnya = array('q=','p='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } } $keyword = strtolower($keyword); $urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'"); $keyword = str_replace($urikeyword, "", "$keyword"); echo "$keyword"; exit; ?> Many Thanks Natasha T Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/ Share on other sites More sharing options...
micah1701 Posted October 23, 2010 Share Posted October 23, 2010 I hate to be one of those people who writes a comment that doesn't answer your question.... but.... you're better off saving all the keywords in a database. If you used an array you would need to then convert it to a string and store it in a flat file somewhere on your site, then when you wanted to view the data, convert it back to an array and parse through it programatically. A database would be much easier and provide simple searchability as well. Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1125565 Share on other sites More sharing options...
natasha_thomas Posted October 23, 2010 Author Share Posted October 23, 2010 I hate to be one of those people who writes a comment that doesn't answer your question.... but.... you're better off saving all the keywords in a database. If you used an array you would need to then convert it to a string and store it in a flat file somewhere on your site, then when you wanted to view the data, convert it back to an array and parse through it programatically. A database would be much easier and provide simple searchability as well. What you said make lot of sense. I do not want Database because of some reasons. But saving it all in a flatfile (.txt) and fetching back from Text file make sense. So, could you help me with the codes as in, how to save the $keyword whenever a New Keyword s Extracted from URL. and displaying all the keywords form the flat file on page. Will be Grateful for your help. Natasha T Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1125584 Share on other sites More sharing options...
.josh Posted October 23, 2010 Share Posted October 23, 2010 Is there a particular reason you're trying to reinvent the wheel? Tools like Google Analytics, Yahoo Web Analytics, etc... are around for just such reasons as this. Even out-of-the-box, just throwing the default coding on all your pages, will answer those questions for you. Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1125589 Share on other sites More sharing options...
.josh Posted October 23, 2010 Share Posted October 23, 2010 but anyways..if you insist on moving forward with your plan...if you are looking to save an array to a flatfile and retrieve and add to it, etc... look into serialize and unserialize. Basically you would do something like : // get current list from your file $keywords = file_get_contents('file.txt'); $keywords = unserialize($keywords); // add new keyword to list $keywords[] = $keyword; // put it back into text file $keywords = serialize($keywords); file_put_contents('file.txt',$keywords); But depending on traffic and what kind of history of keywords you want to keep track of...file could get to large for something like this. Will have to read /write one line at a time with fopen/fread/fwrite. But even then...using a database is your best overall option. Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1125591 Share on other sites More sharing options...
natasha_thomas Posted October 23, 2010 Author Share Posted October 23, 2010 but anyways..if you insist on moving forward with your plan...if you are looking to save an array to a flatfile and retrieve and add to it, etc... look into serialize and unserialize. Basically you would do something like : // get current list from your file $keywords = file_get_contents('file.txt'); $keywords = unserialize($keywords); // add new keyword to list $keywords[] = $keyword; // put it back into text file $keywords = serialize($keywords); file_put_contents('file.txt',$keywords); But depending on traffic and what kind of history of keywords you want to keep track of...file could get to large for something like this. Will have to read /write one line at a time with fopen/fread/fwrite. But even then...using a database is your best overall option. Dear Crayon, My purpose is not to track Keywords but to Hyperlink those Keywords in my Web pages for SEO Purposes, so i can rank higher for these keywords which i am not optimized for but still get traffic for. I thank you for your script. My observations: After adding your codes the output is like: a:2:{i:0;s:8:"keyword1";i:1;s:8:"keyword2";} It should rather be like: keyword1 keyword2 And, i want to hyperlink each of these outputs so is it posible to call these keywords from Flatflile with Foreach() or so.... I want to hyperlink each output keyword with this code: $keywordname_url = stripslashes(str_replace('+', '-', urlencode($keywordname))); $navigation.= "<class='departments'><a href='".($keywordname_url).".htm'>".Ucwords($keywordname)."</a> | "; What code changes shall we do? Cheers Natasha T Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1125642 Share on other sites More sharing options...
.josh Posted October 23, 2010 Share Posted October 23, 2010 okay then as mentioned, you need to use fopen, fread and fwrite along with a loop to loop through each word on in the file. Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1125644 Share on other sites More sharing options...
natasha_thomas Posted October 23, 2010 Author Share Posted October 23, 2010 okay then as mentioned, you need to use fopen, fread and fwrite along with a loop to loop through each word on in the file. Dear Crayon, I have come out with $fp = fopen('file.txt', 'w'); fwrite($fp, $keyword); while (!feof($fp)) { $content .= fread($fp, 1024); } fclose($fp); foreach ($content as $kw) { echo $kw; } I am not able to make it work Could you tell me, what changes in the code required? Cheers NT Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1125655 Share on other sites More sharing options...
natasha_thomas Posted October 25, 2010 Author Share Posted October 25, 2010 May I Seek help on this From Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126047 Share on other sites More sharing options...
darkfreaks Posted October 25, 2010 Share Posted October 25, 2010 <?php $filename = 'test.txt'; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $kewyword) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($keyword) to file ($filename)"; while (!feof($filename) { $content .= fread($fp, 1024); } fclose($fp); foreach ($content as $kw) { echo $kw; } else { echo "The file $filename is not writable"; } ?> Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126050 Share on other sites More sharing options...
natasha_thomas Posted October 25, 2010 Author Share Posted October 25, 2010 <?php $filename = 'test.txt'; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $kewyword) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($keyword) to file ($filename)"; while (!feof($filename) { $content .= fread($fp, 1024); } fclose($fp); foreach ($content as $kw) { echo $kw; } else { echo "The file $filename is not writable"; } ?> Hope this helps Many Thanks darkFresk Based on your code, here is my Full Code, which Giving an Error: <? $referrer = $_SERVER['HTTP_REFERER']; $referrer ='http://www.google.com/search?hl=en&source=hp&biw=1109&bih=736&q=law+of+attraction&aq=f&aqi=&aql=&oq=&gs_rfai='; $referrer_query = parse_url($referrer); $referrer_host = $referrer_query['host']; $referrer_query = $referrer_query['query']; $q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword); $keyword = urldecode($keyword[1]); $potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } if ($keyword=="") { $potongnya = array('q=','p='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } } $keyword = strtolower($keyword); $urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'"); $keyword = str_replace($urikeyword, "", "$keyword"); $filename = 'file.txt'; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $kewyword) === FALSE) { //echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($keyword) to file ($filename)"; while (!feof($filename)) { $content .= fread($fp, 1024); } fclose($fp); foreach ($content as $kw) { echo $kw; } ?> Error Message: Parse error: syntax error, unexpected $end in /home/******/public_html/master/dev/kwlogger.php on line 83 May you help me spotting the Error? Cheers Natasha T Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126054 Share on other sites More sharing options...
kenrbnsn Posted October 25, 2010 Share Posted October 25, 2010 You have an opening "{" on this line: <?php if (is_writable($filename)) { ?> but no closing "}". You can rewrite the two blocks <?php if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } ?> as <?php if ($posnyaaa !== false) { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126057 Share on other sites More sharing options...
natasha_thomas Posted October 25, 2010 Author Share Posted October 25, 2010 You have an opening "{" on this line: <?php if (is_writable($filename)) { ?> but no closing "}". You can rewrite the two blocks <?php if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } ?> as <?php if ($posnyaaa !== false) { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } ?> Ken Dear ken, I have applied your first suggestions and removed the File Writablity Check completely, as its not required, Here is the code aftr that: <? $referrer = $_SERVER['HTTP_REFERER']; $referrer ='http://www.google.com/search?hl=en&source=hp&biw=1109&bih=736&q=law+of+attraction&aq=f&aqi=&aql=&oq=&gs_rfai='; $referrer_query = parse_url($referrer); $referrer_host = $referrer_query['host']; $referrer_query = $referrer_query['query']; $q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword); $keyword = urldecode($keyword[1]); $potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } if ($keyword=="") { $potongnya = array('q=','p='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } } $keyword = strtolower($keyword); $urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'"); $keyword = str_replace($urikeyword, "", "$keyword"); $filename = 'file.txt'; // Let's make sure the file exists and is writable first. if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $kewyword) === FALSE) { //echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($keyword) to file ($filename)"; while (!feof($filename)) { $content .= fread($fp, 1024); } fclose($fp); foreach ($content as $kw) { echo $kw; } ?> Once run, there is an Infinite loop Going on Fread(0 Output: Success, wrote (law of attraction) to file (file.txt) Warning: feof(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 75 Warning: fread(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 76 Warning: feof(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 75 Warning: fread(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 76 Warning: feof(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 75 Warning: fread(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 76 Warning: feof(): supplied argument is not a valid stream resource in /home/*****/public_html/master/dev/kwlogger.php on line 75 So on............... What could be wrong? Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126060 Share on other sites More sharing options...
kenrbnsn Posted October 25, 2010 Share Posted October 25, 2010 You open the file with <?php $handle = fopen($filename, 'w') ?> so you need to use the variable $handle when ever you reference the opened file. Change <?php while (!feof($filename)) { $content .= fread($fp, 1024); } fclose($fp); ?> to <?php while (!feof($handle)) { $content .= fread($handle, 1024); } fclose($handle); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126064 Share on other sites More sharing options...
natasha_thomas Posted October 25, 2010 Author Share Posted October 25, 2010 You open the file with <?php $handle = fopen($filename, 'w') ?> so you need to use the variable $handle when ever you reference the opened file. Change <?php while (!feof($filename)) { $content .= fread($fp, 1024); } fclose($fp); ?> to <?php while (!feof($handle)) { $content .= fread($handle, 1024); } fclose($handle); ?> Ken As you asked, i have changed the codes.... Here they are: <? $referrer = $_SERVER['HTTP_REFERER']; $referrer ='http://www.google.com/search?hl=en&source=hp&biw=1109&bih=736&q=law+of+attraction&aq=f&aqi=&aql=&oq=&gs_rfai='; $referrer_query = parse_url($referrer); $referrer_host = $referrer_query['host']; $referrer_query = $referrer_query['query']; $q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword); $keyword = urldecode($keyword[1]); $potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } if ($keyword=="") { $potongnya = array('q=','p='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } } $keyword = strtolower($keyword); $urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'"); $keyword = str_replace($urikeyword, "", "$keyword"); $filename = 'file.txt'; // Let's make sure the file exists and is writable first. if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $kewyword) === FALSE) { //echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($keyword) to file ($filename)"; while (!feof($handle)) { $content .= fread($handle, 1024); } fclose($handle); foreach ($content as $kw) { echo $kw; } ?> Output: Success, wrote (law of attraction) to file (file.txt) 1- Its does not write anything to File.txt, i opend and confirmed. - File is already 755 CHMOD and does exist On another Note, there is an Infinite loop Again.. without any eror message this time.... :-\ Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126067 Share on other sites More sharing options...
darkfreaks Posted October 25, 2010 Share Posted October 25, 2010 Try using 777 Permissions Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126274 Share on other sites More sharing options...
natasha_thomas Posted October 25, 2010 Author Share Posted October 25, 2010 Try using 777 Permissions Dear DarkFresk, Am really struggling my way through to make it work. :'( Here is the code am suing.. <? $referrer = $_SERVER['HTTP_REFERER']; $referrer ='http://www.google.com/search?hl=en&source=hp&biw=1109&bih=736&q=law+of+attraction&aq=f&aqi=&aql=&oq=&gs_rfai='; $referrer_query = parse_url($referrer); $referrer_host = $referrer_query['host']; $referrer_query = $referrer_query['query']; $q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword); $keyword = urldecode($keyword[1]); $potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } if ($keyword=="") { $potongnya = array('q=','p='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } } $keyword = strtolower($keyword); $urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'"); $keyword = str_replace($urikeyword, "", "$keyword"); $filename = 'file.txt'; // Let's make sure the file exists and is writable first. if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $kewyword) === FALSE) { //echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($keyword) to file ($filename)"; while (!feof($handle)) { $content .= fread($handle, 1024); } fclose($handle); foreach ($content as $kw) { echo $kw; } ?> Output: Success, wrote (law of attraction) to file (file.txt) But, There is an Infinite Loop going once it echos the Above Message. Nothing is Written in file.txt, i have tried 755, 777 both. May you plz help me with This? Cheers Natasha T Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126310 Share on other sites More sharing options...
kenrbnsn Posted October 25, 2010 Share Posted October 25, 2010 You misspelled the variable name "$keyword" in this line: <?php if (fwrite($handle, $kewyword) === FALSE) { ?> so nothing is being written to your file. I would also add a end-of-line character after the data being written: <?php if (fwrite($handle, $keyword . "\n") === FALSE) { ?> The reason for the apparent infinite loop is that you're trying to read the file before you close it and the code is blocking the read. Change <?php while (!feof($handle)) { $content .= fread($handle, 1024); } fclose($handle); ?> to <?php fclose($handle); $content = file('file.txt'); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126330 Share on other sites More sharing options...
natasha_thomas Posted October 25, 2010 Author Share Posted October 25, 2010 You misspelled the variable name "$keyword" in this line: <?php if (fwrite($handle, $kewyword) === FALSE) { ?> so nothing is being written to your file. I would also add a end-of-line character after the data being written: <?php if (fwrite($handle, $keyword . "\n") === FALSE) { ?> The reason for the apparent infinite loop is that you're trying to read the file before you close it and the code is blocking the read. Change <?php while (!feof($handle)) { $content .= fread($handle, 1024); } fclose($handle); ?> to <?php fclose($handle); $content = file('file.txt'); ?> Ken Based on what all you said, here is my New Code <? $referrer = $_SERVER['HTTP_REFERER']; $referrer ='http://www.google.com/search?hl=en&source=hp&biw=1109&bih=736&q=hehheh&aq=f&aqi=&aql=&oq=&gs_rfai='; $referrer_query = parse_url($referrer); $referrer_host = $referrer_query['host']; $referrer_query = $referrer_query['query']; $q = "[q|p|qkw|key|query|searchfor|Keywords|searchterm]"; //Yahoo uses both query strings, I am using switch() for each search engine preg_match('/'.$q.'=(.*?)&/',$referrer,$keyword); $keyword = urldecode($keyword[1]); $potongnya = array('&q=','&p=','key=','query=','searchfor=','Keywords=','searchterm='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } if ($keyword=="") { $potongnya = array('q=','p='); $referrer_query = str_replace($potongnya, "&|pencarian=", $referrer_query); $arr = explode("&", "$referrer_query"); for($k = 0, $l = count($arr); $k < $l; ++$k) { $ygdicari = "$arr[$k]"; $findmeyach = "|pencarian="; $posnyaaa = strpos($ygdicari, $findmeyach); if ($posnyaaa === false) {} else { $keyword = str_replace('|pencarian=', "", $ygdicari); $potongkeynya = array('+','%20'); $keyword = str_replace($potongkeynya, " ", $keyword); } } } $keyword = strtolower($keyword); $urikeyword = array('sex ','porn ','porno',', ','penis ','penis ','http://www.','http://','http','gamble','"',"'"); $keyword = str_replace($urikeyword, "", "$keyword"); $filename = 'file.txt'; // Let's make sure the file exists and is writable first. if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $keyword . "\n") === FALSE) { //echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($keyword) to file ($filename)"; fclose($handle); $content = file('file.txt'); foreach ($content as $kw) { echo $kw; } ?> Observations: Its Adding Keyword to text file but its Overwriting any existing value in Text file, it should rather Append, so i can build up a list of keywords in text file. What changes we need? Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126343 Share on other sites More sharing options...
darkfreaks Posted October 25, 2010 Share Posted October 25, 2010 i read in a tutorial if you replace fopen($file,'w'); with fopen($file,'a'); //append it will avoid wiping the text Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126349 Share on other sites More sharing options...
natasha_thomas Posted October 25, 2010 Author Share Posted October 25, 2010 I want to Thank Ken and darkFreak... Both have halped me much to achieve this... One more thing.... In the Above Code, if i want to pull only 10 Keywords Randomly at a time and put them in an Array , so that, i can again do foreach() on that new Array to echo the filtered array..... Possible? Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126360 Share on other sites More sharing options...
darkfreaks Posted October 25, 2010 Share Posted October 25, 2010 $keywords=array(); //keyword array $kwywords.= array_rand($keywords,10) //picking 10 random keywords foreach($keywords as $k){ //looping echo $k; } Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126363 Share on other sites More sharing options...
natasha_thomas Posted October 25, 2010 Author Share Posted October 25, 2010 Many Thanks to darkfrek. Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126369 Share on other sites More sharing options...
darkfreaks Posted October 25, 2010 Share Posted October 25, 2010 $content=file('test.txt'); //reads file into array $content.= array_rand($content,10); // pull 10 random keywords from array foreach($content as $kw) { //loop array } Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126372 Share on other sites More sharing options...
darkfreaks Posted October 25, 2010 Share Posted October 25, 2010 that's my corrected and final code. i used keyword instead of content i was not paying attention. Quote Link to comment https://forums.phpfreaks.com/topic/216642-saving-keywords-in-an-array/#findComment-1126381 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.