如何在一个变量中结合数组值
问题描述:
因此,我开始研究这个twitter脚本,该脚本针对的是推特关于我的网站的用户,并且我希望通过cron工作来设置它以感谢他们这样做。这是如何工作的:如何在一个变量中结合数组值
通过所有15个用户名解析来自搜索页(共15)
循环的用户名和如果在twitter.txt文件 发现它会停止脚本
如果在twitter.txt文件中找不到用户名,它会将用户名 写入twitter.txt文件,然后向该用户发送推文。 (twitter.txt文件有助于防止重复发送微博的同一个用户)
所以我的问题是,这个脚本目前发出鸣叫15在一次会议,这将被视为垃圾邮件。所以我需要帮助才能将用户名合并为3条推文。所以每条推文将包含5个用户名。你将在脚本的底部看到我有变量存储用户名的推文。
//cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://search.twitter.com/search.atom?q=MYWEBSITE");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$twitter = curl_exec($curl);
curl_close($curl);
//search html source page for user names
preg_match_all('/\<uri\>http\:\/\/twitter\.com\/(.*?)\<\/uri\>/', $twitter, $usernames);
//open the twitter text file and prepare for writing
$twitter = fopen("twitter.txt", "a");
//contents of the twitter text file (holds the twitter user names)
$contents = file_get_contents("twitter.txt");
//loop through each user name
foreach($usernames[1] as $username) {
//if user name is found in the twitter text file it states "Found" and script stops
if (strpos($contents, $username) !== FALSE) {
echo $username . " - <font color=\"red\">Found</font><br />";
//if user name is not found in the twitter text file it records the user name and tweets
} else {
//write to twitter text file
fwrite($twitter, $username."\r\n");
//shows what user names were recorded
echo $username . "<br />";
//user names for tweets
//this is where i need help dividing the usernames into these variables
$usernames_set_one = "";
$usernames_set_two = "";
$usernames_set_three = "";
//tweet
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get('account/verify_credentials');
//twitter message
$connection->post('statuses/update', array('status' => $usernames_set_one . ' thank you for tweeting about my website.'));
$connection->post('statuses/update', array('status' => $usernames_set_two . ' thank you for tweeting about my website.'));
$connection->post('statuses/update', array('status' => $usernames_set_three . ' thank you for tweeting about my website.'));
return $connection;
}
}
//close the twitter text file no further writing
fclose($twitter);
答
可以使用的while loop
代替foreach
然后访问用户名是:
$用户名[1] [$ i];
$ usernames [1] [$ i + 1];
$ usernames [1] [$ i + 2];
例如:
$i = 0;
$count = count($usernames[1]);
while($i < $count - 3) //we will use $i+2 after all
{
$i++;
}
答
改变那些3个变量阵列以及当所述第一个是完整的用户名添加至所述第二数组,然后到第三。发布到twitter时,只需将姓名加入implode()
即可。
我会使用array_fill()来第一部分? – jeff 2012-03-10 11:34:09
对于第一部分,您可以使用'array_push($ username_set_one,$ username)',但在此之前,您需要检查您是否已经在该数组中有三个名称,然后转到下一个。 'count($ username_set_one)'会给你该数组中的总项目。 – slash197 2012-03-10 11:47:08