The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 16, 2019, 06:03:18 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: I'm trying to code a ...  (Read 2917 times)
NYDAz
Expert
****
Offline Offline

Posts: 212

The Night Stalker


View Profile
« on: April 13, 2009, 02:32:23 PM »

Guys could you help me ? I haven't too much inspiration right now or I don't know much about PHP  D'oh!

But here is what i'm trying to build : a little script that can generate long keywords !

Let's say I begin with 5 main keywords : example1, example2, example 3, example4 and example 5

Code:
<?php
$var1 
"example1";
$var2 "example2";
$var3 "example3";
$var4 "example4";
$var5 "example5";

Now I want to append to all this keywords "free shipping"

I've done it like this :
Code:
$free = "free shipping";
echo $var1.$free."</br>;
echo $var2.$free."</br>;
echo $var3.$free."</br>;
echo $var4.$free."</br>;
echo $var5.$free."</br>;
?>

I know this is like   for coders like you, but hey I want to learn too  Praise

The next thing i would like to do is to include other additions to keywords like "low prices" "cheap" "you got it" but I want them to store them in a .txt file and to call the txt file from PHP and to append each addition to each keyword !

Thoughts ?  Smiley
Logged

what's up?
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #1 on: April 13, 2009, 02:52:54 PM »

Code:
<?php
$myKeywords 
= array('hats''funny hats''black hats');
$prefix 'cheap';
$outString '';
foreach(
$myKeywords as $keyword){
   
$outString .= $prefix ' ' $keyword "\n";
}
file_put_contents('out.txt'$outString);
?>


EDIT: Also, if you have your keywords in a file (one per line) called "keywords.txt", you can just load them in like $myKeywords = file('keywords.txt');
Logged

hai
NYDAz
Expert
****
Offline Offline

Posts: 212

The Night Stalker


View Profile
« Reply #2 on: April 13, 2009, 02:57:51 PM »

Wow thanks vsloathe 

By the way i've made of function to append each keyword any state from US !

Code:
<?php
function keywordState($state){
$var1 "example1";
$var2 "example2";
$var3 "example3";
$var4 "example4";
$var5 "example5";
echo $var1." ".$state."</br/>";
echo $var2." ".$state."</br/>";
echo $var3." ".$state."</br/>";
echo $var4." ".$state."</br/>";
echo $var5." ".$state."</br/>";
}
keywordState("Alabama");
keywordState("Alaska");
keywordState("American Samoa");
keywordState("Arizona");
keywordState("Arkansas");
keywordState("California");
keywordState("Colorado");
keywordState("Connecticut");
keywordState("Delaware");
keywordState("District of Columbia");
keywordState("Florida");
keywordState("Georgia");
keywordState("Guam");
keywordState("Hawaii");
keywordState("Idaho");
keywordState("llinois");
keywordState("Indiana");
keywordState("Iowa");
keywordState("Kansas");
keywordState("Kentucky");
keywordState("Louisiana");
keywordState("Maine");
keywordState("Maryland");
keywordState("Massachusetts");
keywordState("Michigan");
keywordState("Minnesota");
keywordState("Mississippi");
keywordState("Missouri");
keywordState("Montana");
keywordState("Nebraska");
keywordState("Nevada");
keywordState("New Hampshire");
keywordState("New Jersey");
keywordState("New Mexico");
keywordState("NewYork");
keywordState("North Carolina");
keywordState("North Dakota");
keywordState("Northern Marianas Islands");
keywordState("Ohio");
keywordState("Oklahoma");
keywordState("Oregon");
keywordState("Pennsylvania");
keywordState("Puerto Rico");
keywordState("Rhode Island");
keywordState("South Carolina");
keywordState("South Dakota");
keywordState("Tennessee");
keywordState("Texas");
keywordState("Utah");
keywordState("Vermont");
keywordState("Virginia");
keywordState("Virgin Islands");
keywordState("Washington");
keywordState("West Virginia");
keywordState("Wisconsin");
keywordState("Wyoming");
?>


What do you think ?  Cry

By the way in your example I want to call $prefix content from a file ! How can I do this ?  D'oh!
Logged

what's up?
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« Reply #3 on: April 14, 2009, 01:49:19 AM »

Hi NYDAz,

You could do it using the same method VS provided, but just create another foreach loop. e.g.

Code:
$prefixes = file('myPrefixes.txt');
$keywords = file('keywords.txt');

foreach($prefixes as $prefix) {
    $outString = '';
    foreach($keywords as $keyword) {
        $outString .= $prefix . ' ' . $keyword . "\n";
    }
    file_put_contents($prefix . '-out.txt', $outString);
}

There are possibly better solutions, and obviously that code includes no error checking.

Might I suggest you take a look at the PHP manual on arrays - http://uk.php.net/array and loops - http://uk3.php.net/manual/en/control-structures.foreach.php, I think you'll find it enlightening.

Cheers,

DM
« Last Edit: April 14, 2009, 01:55:46 AM by DangerMouse » Logged
NYDAz
Expert
****
Offline Offline

Posts: 212

The Night Stalker


View Profile
« Reply #4 on: April 14, 2009, 03:25:20 AM »

Hi NYDAz,

You could do it using the same method VS provided, but just create another foreach loop. e.g.

Code:
$prefixes = file('myPrefixes.txt');
$keywords = file('keywords.txt');

foreach($prefixes as $prefix) {
    $outString = '';
    foreach($keywords as $keyword) {
        $outString .= $prefix . ' ' . $keyword . "\n";
    }
    file_put_contents($prefix . '-out.txt', $outString);
}

There are possibly better solutions, and obviously that code includes no error checking.

Might I suggest you take a look at the PHP manual on arrays - http://uk.php.net/array and loops - http://uk3.php.net/manual/en/control-structures.foreach.php, I think you'll find it enlightening.

Cheers,

DM

I was trying to make 2 statments in the first each loop and that doesn't work  D'oh!

Your tip is very very handy DangerMouse 

Anyway I was able to create some kind of thing with my knowledge  Roll Eyes

Code:
<?php
$var1 
"keyword"// <- Insert here your keyword
$buy "buy $var1";
$cheap "cheap $var1";
$order "order $var1";
    echo 
$buy."</br/>"// Prefix
echo $cheap."</br/>"// Prefix
echo $order."</br/>"// Prefix
function keywordState($state){
$var2 "keyword"// <- Again, insert your keyword

$freeShipping "free shipping"// Sufix 
echo $var2." ".$state."</br/>";
echo $var2." ".$state." ".$freeShipping."</br/>";

}
keywordState("Alabama");
keywordState("Alaska");
keywordState("American Samoa");
keywordState("Arizona");
keywordState("Arkansas");
keywordState("California");
keywordState("Colorado");
keywordState("Connecticut");
keywordState("Delaware");
keywordState("District of Columbia");
keywordState("Florida");
keywordState("Georgia");
keywordState("Guam");
keywordState("Hawaii");
keywordState("Idaho");
keywordState("llinois");
keywordState("Indiana");
keywordState("Iowa");
keywordState("Kansas");
keywordState("Kentucky");
keywordState("Louisiana");
keywordState("Maine");
keywordState("Maryland");
keywordState("Massachusetts");
keywordState("Michigan");
keywordState("Minnesota");
keywordState("Mississippi");
keywordState("Missouri");
keywordState("Montana");
keywordState("Nebraska");
keywordState("Nevada");
keywordState("New Hampshire");
keywordState("New Jersey");
keywordState("New Mexico");
keywordState("NewYork");
keywordState("North Carolina");
keywordState("North Dakota");
keywordState("Northern Marianas Islands");
keywordState("Ohio");
keywordState("Oklahoma");
keywordState("Oregon");
keywordState("Pennsylvania");
keywordState("Puerto Rico");
keywordState("Rhode Island");
keywordState("South Carolina");
keywordState("South Dakota");
keywordState("Tennessee");
keywordState("Texas");
keywordState("Utah");
keywordState("Vermont");
keywordState("Virginia");
keywordState("Virgin Islands");
keywordState("Washington");
keywordState("West Virginia");
keywordState("Wisconsin");
keywordState("Wyoming");
?>


Is too long I know ... but I'm learning from the pros  Wink

Thanks again perkiset, vsloathe and DangerMouse !

My second script 
Logged

what's up?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: April 14, 2009, 10:56:14 AM »

NYD - consider this way of doing things:

Code:
<?php

$prefixes 
= array('buy''purchase''order''cheap''best price on');
$keyword 'viagra';
$states = array('Alabama''Alaska''Arizona''Arkansas'); // Obviously, add all states here

$outArr = array();
foreach(
$prefixes as $prefix)
{
foreach($states as $state)
{
$outArr[] = "$prefix $keyword in $state";
}
}

$outStr implode(chr(10), $outArr);
file_put_contents('./keyphrases.txt'$outStr);
?>


That'd go really fast and is obviously extensible. Want to add another layer? Simply add another array and nested loop.

Good luck!
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
NYDAz
Expert
****
Offline Offline

Posts: 212

The Night Stalker


View Profile
« Reply #6 on: April 14, 2009, 01:36:18 PM »

That's some nasty piece of code 

I've modified to my needs :

Code:
foreach ($sufixes as $sufix)
{
foreach ($prefixes as $prefix)
{
$outArr[] = "$prefix $keyword";
}
foreach ($states as $state)
{
$outArr[] = "$keyword $state";
$outArr[] = "$keyword $state $sufix";
}
}

By the way what means implode (chr(10), $outArr)Need Help
Logged

what's up?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #7 on: April 14, 2009, 02:09:32 PM »

implode converts an array into a big ol' string, separated by the first parameter - in this case, a chr(10) or carriage return. It's the opposite of explode.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
NYDAz
Expert
****
Offline Offline

Posts: 212

The Night Stalker


View Profile
« Reply #8 on: April 14, 2009, 02:13:39 PM »

implode converts an array into a big ol' string, separated by the first parameter - in this case, a chr(10) or carriage return. It's the opposite of explode.
Big THANKS perk  Praise
Logged

what's up?
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!