Jump to content

Saving keywords in an Array


natasha_thomas

Recommended Posts

@Darkfresk - Yes i was aware of it and debugged it yesterday only. Thanks

 

Another thing, i have implemented the Code on my Production Site, but now i see another problem..

 

Thing is, when the visitor comes from Search Engine it detects KW, and KW is written on File.txt, then visitor goes to site and then if he clicks on anylink on my Site, a " "  Blank Space is written back to file.txt... Strange...

 

Infact, this codde should write KW in file.txt only first time when the visitor comes to Site. adn no when i click the internal links on my Site.

 

Make sense?

 

Here are the codes:

<?
//$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, 'a')) {
         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');


$contentnew=array();
$contentnew= array_rand($content,5);
foreach($contentnew as $k)
{
echo $k;
$referrer = null;
} ?>

 

Could you help me with this?

 

Natasha T

Link to comment
Share on other sites

The blank space you're seeing is actually the newline character on each record. If you do a "show source", you will see that each keyword is on a separate line.

 

If you want to show each keyword on a separate line on the screen, you should do

<?php
foreach($contentnew as $k) {
    echo nl2br($k);
}
$referrer = null;
?>

 

If you want to separate each keyword with a comma or some other character, do

<?php
$content = array_filter(array_map('trim',file('file.txt')));
shuffle($content);
echo implode(', ',array_slice($content,0,5));
?>

 

Ken

 

Ken

Link to comment
Share on other sites

The blank space you're seeing is actually the newline character on each record. If you do a "show source", you will see that each keyword is on a separate line.

 

If you want to show each keyword on a separate line on the screen, you should do

<?php
foreach($contentnew as $k) {
    echo nl2br($k);
}
$referrer = null;
?>

 

If you want to separate each keyword with a comma or some other character, do

<?php
$content = array_filter(array_map('trim',file('file.txt')));
shuffle($content);
echo implode(', ',array_slice($content,0,5));
?>

 

Ken

 

Ken

Dear  Ken,

 

I really did not quite understood your reply. Apolozies.

 

What is happening is, Whenever i click on a Link on my Site a New Line character is appended in File.txt

Actually, when the visitor first lands on the Page only that KW should be logged in file.txt.

THats all I want...

 

Just wondering, by adding the codes you just gave, will i be able to achieve what am looking for?

 

Many Thanks

Link to comment
Share on other sites

let me comment kens code so you understand

 

<?php
foreach($contentnew as $k) {
    echo nl2br($k); //echo content with line breaks <br>
}
$referrer = null; //reffer equals nothing
?>

 

so the content will look like

Johny

Joe

 

instead of

 

Johny Joe

also if you do not want to use  nbr12() function you can do

echo "$k/n"; //line break <br>

 

this will do essentially the same thing as the function.

 

next we use kens  array_filter and array_map functions to trim blank space out of the file.

 

$content = array_filter(array_map('trim',file('file.txt')));

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.