Undefined variable on php script - php

I am getting notice on line 30 that twetout is undefined variable
<?php
$username = "tomaskutaj";/*
$limit = 5;
$feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name='.$username.'&count='.$limit;*/
$feed ="http://search.twitter.com/search.rss?q=#tomaskutaj";
$tweets = file_get_contents($feed);
$tweets = str_replace("&", "&", $tweets);
$tweets = str_replace("<", "<", $tweets);
$tweets = str_replace(">", ">", $tweets);
$tweet = explode("<item>", $tweets);
$tcount = count($tweet) - 1;
for ($i = 1; $i <= $tcount; $i++) {
$endtweet = explode("</item>", $tweet[$i]);
$title = explode("<title>", $endtweet[0]);
$content = explode("</title>", $title[1]);
$content[0] = str_replace("–", "—", $content[0]);
$content[0] = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", '$1$2$4', $content[0]);
$content[0] = str_replace("$username: ", "", $content[0]);
$content[0] = preg_replace("/#(\w+)/", "#\\1", $content[0]);
$content[0] = preg_replace("/#(\w+)/", "#\\1", $content[0]);
$mytweets[] = $content[0];
}
$x=1;
while (list(, $v) = each($mytweets)) {
$tweetout .= "<div>$v</div>\n";
if ($x==1){
$first=$tweetout;
};
$x++;
}
if ((strstr($first,'#tomaskutaj'))&&(strstr($first,'#tomaskutaj')))
echo($first);
?>
But after I added before last while saying '$tweetout='';' it runs the script but doesnt get any output and no error either, where is the problem?

The problem is that you are trying to append to a variable that is not defined.
$tweetout .= "<div>$v</div>\n";
PHP gives you a notice, as in most cases, this happens because you typo-ed a variable name. You don't get any errors, as PHP just assumes you meant to append to null and continues.
To resolve the issue, before your while loop, define $tweetout:
$tweetout = '';

You should definitely add a
$tweetout = "";
before your loop, appending to a non existing string is an error.
The reason you're getting nothing on the screen is that $first does not contain #tomaskutaj, and the if-statement at the end requires that (and #tomaskutaj which is there) to print anything.

Related

PHP trim and keep only first and last part

I have variables as follow in PHP
steve_s_baue
marine_camp_se_bell
mike_wane
I want to only keep the first and last part:
steve_baue
marine_bell
mike_wane
I tried to use trim but got stuck.
EDIT: Here is what I tried to far
$row = $pre_results[$i];
$name = $row -> name;
$text = preg_replace('~[^\pL\d]+~u', '_', $name);
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = preg_replace('~[^-\w]+~', '', $text);
$text = trim($text, '_');
$text = preg_replace('~-+~', '_', $text);
$text = strtolower($text);
Any suggestions?
function firstlast($var,$seperator) {
$varr = explode($seperator, $var);
$first = current($varr);
$last = end($varr);
return $first.'_'.$last;
}
$seperator='_';
$old = 'marine_camp_se_bell';
$new = firstlast($old, $seperator);
echo $new;
If you put original strings in an array, you can run array_walk(); with this function
You can check this:
http://nimb.ws/dVHzZJ and
http://nimb.ws/ENgTCm
<?php
$a = "steve_s_baue";
$temp=explode("_",$a);
$arr=array($temp[0],$temp[2]);
print_r(implode("_",$arr));
?>
Do it with explode function, I made a basic example working
<?php
$name = "marine_camp_se_bell";
$var = explode("_",$name);
print_r (explode("_",$name));
//than print as array
echo "</br>";
echo $var[0];
echo "</br>";
echo $var[3];
?>
$name = "john_dont_do_some_doe";
$array = explode("_", $name);
$last = count($array) - 1;
echo $array[0]."_". $array[$last]; //shows: john_doe
*not tested, but should do the job

the web page is temporarily down or it may have moved permanently to another address. ERR_RESPONSE_HEADERS_TRUNCATED

I'm trying to get an RSS feed, I remove all stopwords, stemm... but it works sometimes. I do not know what is happening. Could anyone see my code and tell me where is the error?. Please
functions.php
<?php
include ("stemm_es.php");
function quitarAcento($incoming_string){
$tofind = "ÀÁÂÄÅàáâäÒÓÔÖòóôöÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ";
$replac = "AAAAAaaaaOOOOooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn";
return utf8_encode(strtr(utf8_decode($incoming_string),
utf8_decode($tofind),
$replac));
}
function limpiar($String){
$String = str_replace(array('á','à','â','ã','ª','ä'),"a",$String);
$String = str_replace(array('Á','À','Â','Ã','Ä'),"A",$String);
$String = str_replace(array('Í','Ì','Î','Ï'),"I",$String);
$String = str_replace(array('í','ì','î','ï'),"i",$String);
$String = str_replace(array('é','è','ê','ë'),"e",$String);
$String = str_replace(array('É','È','Ê','Ë'),"E",$String);
$String = str_replace(array('ó','ò','ô','õ','ö','º'),"o",$String);
$String = str_replace(array('Ó','Ò','Ô','Õ','Ö'),"O",$String);
$String = str_replace(array('ú','ù','û','ü'),"u",$String);
$String = str_replace(array('Ú','Ù','Û','Ü'),"U",$String);
$String = str_replace(array('[','^','´','`','¨','~',']','"',"'",'(',')','{','}',',','.',':',';','%','-',
'#','#','|','!','·','$','&','/','?','¡','¿','+', '”', '“', '’', '‘'),"",$String);
$String = str_replace("ç","c",$String);
$String = str_replace("Ç","C",$String);
$String = str_replace("ñ","n",$String);
$String = str_replace("Ñ","N",$String);
$String = str_replace("Ý","Y",$String);
$String = str_replace("ý","y",$String);
$String = str_replace("á","a",$String);
$String = str_replace("Á","A",$String);
$String = str_replace("é","e",$String);
$String = str_replace("É","E",$String);
$String = str_replace("í","i",$String);
$String = str_replace("Í","I",$String);
$String = str_replace("ó","o",$String);
$String = str_replace("Ó","O",$String);
$String = str_replace("ú","u",$String);
$String = str_replace("Ú","U",$String);
return $String;
}
function getFeed($feed_url) {
$x = simplexml_load_string(file_get_contents($feed_url));
$stopwords = file_get_contents('stopword.txt');
$stopwords = explode(" ", $stopwords);
$t=0;
foreach($x->channel->item as $entry){
$t++;
$title = trim( strip_tags( mb_strtolower( limpiar($entry->title) ) ) );
$description = trim( strip_tags( mb_strtolower( limpiar($entry->description) ) ) );
$title = explode(" ", $title);
$description = explode(" ", $description);
for($j = 0; $j < count($title); $j++){
if($title[$j] != ""){
for($i = 0; $i < count($stopwords); $i++){
if($stopwords[$i] != ""){
$title[$j] = preg_replace("/\b$stopwords[$i]\b/i", " ", $title[$j]);
$title[$j] = stemm_es::stemm($title[$j]);
}
}
}
}
for($j = 0; $j < count($description); $j++){
if($description[$j] != ""){
for($i = 0; $i < count($stopwords); $i++){
if($stopwords[$i] != ""){
$description[$j] = preg_replace("/\b$stopwords[$i]\b/i", " ", $description[$j]);
$description[$j] = stemm_es::stemm($description[$j]);
}
}
}
}
print_r($title);
print_r($description);
}
}
?>
index.php
<!DOCTYPE html>
<html class="no-js" lang="es">
<head>
<meta charset="utf-8">
<title>Inicio | Investigador</title>
</head>
<body>
<?php require "functions.php"; ?>
<?php
$feed = "http://ep00.epimg.net/rss/ccaa/valencia.xml";
if ($feed != "") {
getFeed(feed);
}
?>
</body>
</html>
We've had this issue arise for some users with a recent release. Not sure if it is us or timing with Chrome .
It is only affecting some users, and only in Chrome, and clearing cache has helped most users but not all.
Still searching for a definitely solution to it.
EDIT:
After a looooong time I actually resolved it for me by updating the session config settings in CodeIgniter.
I had to update:
$config['sess_encrypt_cookie'] = FALSE;
to:
$config['sess_encrypt_cookie'] = TRUE;
What was happening was on first login, it would work fine, but the second login was getting some sort of conflict with the cookie.
I didn't find out exactly what the issue was, but this seems to be a Chrome based bug a few people have had over the years, but there's not much around on it.

Weird characters jump out of arrays when used with get file contents

Hi this is a portion of my code which when ever output, the array $data2[0] seems to always output weird characters. It doesn't happens in $data2[1] or $data[2]..??? I been trying to figure this out for 2 days.
<?php
$filename = "../file/attendance_log/1414001189.txt";
$contents = file_get_contents($filename);
$contents = str_replace("\"","",$contents);
$lines = explode("\n", $contents);
$numrows = count($lines);
$x = 0;
for ($numrows; $x < $numrows; $x++)
{
echo $data2[0];
$data2 = explode(",", $lines[$x]);
echo $time = mktime(0,0,1,$data2[1],$data2[0],$data2[2]);
$user_no = $data2[3];
$item_no = $data2[4];
$quantity = $data2[5];
$waste = $data2[6];
$job_no = $data2[7];
}
?>
You're trying to explode and use the first line which has the column names in it
i slightly rewrote what you had to accomodate that and also not use a counter and
setting the default timezone too so you get what i think you were looking for
<?php
$filename = "../file/attendance_log/1414001189.txt";
$contents = file_get_contents($filename);
$contents = str_replace("\"","",$contents);
$lines = explode("\n", $contents);
date_default_timezone_set('UTC');
if (count($lines)){
$lines = array_slice($lines, 1);
foreach ($lines as $line){
$data2 = explode(",", $line);
if (count($data2) == 8){
echo $data2[0];
$time = mktime(0,0,1,$data2[1],$data2[0],$data2[2]);
echo $time . '<br>';
$user_no = $data2[3];
$item_no = $data2[4];
$quantity = $data2[5];
$waste = $data2[6];
$job_no = $data2[7];
}
}
}
?>
I had found the answer. It is because when the user save the file. It saved as Unicode UTF. It should save as Unicode UTF-8, that way there won't be any problems.

PHP - str_replace not working

I am trying to remove a list of words, which I have contained in a .txt, from a file. To do this I am reading both files using file_get_contents into strings and using str_replace.
$names = file_get_contents("countries.txt");
$map = file_get_contents("C:\\xampp\\htdocs\\www\\jvectormap\\map\\worldmap.js");
$array = explode("\n", $names);
foreach($array as $val){
$split = explode(" ", $val);
$max = count($split);
$country = "";
for($x = 1; $x < $max; $x++){
$country = $country . $split[$x];
if($x < ($max-1)){
$country = $country . " ";
}
}
$map = str_replace($country, "", $map);
}
echo $map;
The "countries.txt" contains the countries in this format:
AD Andorra
BE Belize
etc.
..which is why I am using explode() to strip the country tag.
When I echo $map the string contains all the countries even thought str_replace hasn't thrown an error. I have tried printing out $country to confirm it's reading the countries correctly along with reading it into an array and then looping through the array, using str_replace.
I think you need some modification in code
change below line
$array = explode("\n", $names);
to with these
$names = nl2br($names);
$array = explode("<br />", $names);
As you are working on window which uses \r\n for new line.
Cannot reproduce.
<?php
/* Instead of loading countries.txt */
$names = "AD Andorra
BE Belize";
$array = explode("\n", $names);
/* Instead of loading map */
$map = "Andorra Belize";
$array = explode("\n", $names);
foreach($array as $val){
$split = explode(" ", $val);
$max = count($split);
$country = "";
for($x = 1; $x < $max; $x++){
$country = $country . $split[$x];
if($x < ($max-1)){
$country = $country . " ";
}
}
$map = str_replace($country, "", $map);
}
var_dump($map);
Output:
string(1) " "
The space is expected, if you want to get rid of it use trim(). However, the replacement is working fine, if it still doesn't work your text files might be the problem.

PHP pick tweets code

I have a php code that fetches my latest tweet and displays it on my website. It works great but I would like to know if I could do this but maybe fetching my 5 last tweets instead of only the latest. This is my code for twitter.php:
function returnTweet()
{
$username = "username";
$prefix = "<div><big><i>#$username ";
$suffix = "</i></big></div>";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=5";
$twitterFeed = file_get_contents($feed);
$tweet = parse_feed($twitterFeed);
return $prefix.$tweet.$suffix;
}
function parse_feed($feed)
{
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace("<", "<", $tweet);
$tweet = str_replace(">", ">", $tweet);
return $tweet;
}
And i display it in my php page like this:
include "twitter.php";
echo "".returnTweet();
Would appreciate any help in this!
Regards from Paparappa
You're much better off using an XML-parser for this (for example: http://www.php.net/manual/en/simplexml.examples-basic.php).
(not sure about the structure of twitter-atoms, so $xml->tweet[$i] is probably something else..)
$xml = new SimpleXMLElement($twitterFeed);
$tweets = array();
for ($i = 0; $i < 5; $i++) {
$tweets[] = $xml->tweet[$i];
}

Categories