I'm putting together a project for class that has to do with aggregating tweets in succession to create a linear, crowdsourced story. I've currently got a semi-working program, with the issue that the program displays the most recent tweet first - what I need is to sort these tweets to have them displayed by timestamp, oldest to newest. Here's the code:
<?php
$url = 'http://search.twitter.com/search.json?q=%23tweetstoryproj&lang=en&rpp=100';
$jsontwitter = file_get_contents($url);
$twitter = json_decode($jsontwitter, true);
$twittertext = $twitter["results"];
foreach($twittertext as $text){
$text = str_replace("#tweetstoryproj", "", $text);
echo $text['text'].'';
}
?>
Thanks a lot for your help, and if you want to contribute to the story, tweet with the hashtag: #tweetstoryproj
Simple! (He says) Just use array_reverse
Add this line in before the foreach
$twittertext = array_reverse($twittertext);
Enjoy!
Alternative:
If the array becomes huge it may take longer to flip it then traverse it, so you could use a "for" loop and go backwards thusly.
for($i=$count($twittertext);$i>-1;$i--) //It's late that may miss the last or the first entry, have a fiddle!
{
// echo here
}
Related
I have a post.I'm making an array explode this post.I have two rules when explode the post.
First Rule: The target must be between a tag.
Second Rule: The words must be explode down according to the space between them.
There is no problem up to this point.You can see it from the code below.
$demo = '<p>This is a red image.Red images do not look good under natural light.This is due to the
saturation rates.
<info><a href="http://localhost/img/red_image.jpg"><img class="danger_image"
src="http://localhost/img/red_image.jpg" alt="red_image_info"/></a>
</info></p>';
$demo = str_replace("<info", "*<info", $demo);
$demo = str_replace("</info>", "</info>*", $demo);
$new_array = array();
foreach (explode('*', $demo) as $demo_loop) {
$new_array[] = explode(' ', $demo_loop);
}
echo '<pre>';
print_r($new_array);
echo '</pre>';
While doing the joining process, I place comment lines between the words.The problem arises after implode the array.
implode(" <!--test--> ",$new_array[$i][$is]);
I complete this implode in a loop.The picture is not displayed while the text is displayed normally.
The output is like this:
This is a red image.Red images do not look good under natural light.This is due to the saturation
rates.
href="http://localhost/img/red_image.jpg"> class="danger_image"
src="http://localhost/img/red_image.jpg" alt="red_image_info"/>
As a result of my experiments, I found that the comments lines caused this.If I can delete these comment lines inside the target tag, I will succeed what I want.
But I got stuck at this point
have you tried the "in_array" function? Maybe you can distinguish this way.
if (in_array("<info>", $new_array)) {
echo "Found! -> Delete Comment Lines";
}else {
echo "Not Found!";
}
You can then delete the comment lines for arrays that do not meet the condition.Remember that this is just an idea.
I am pulling a lot of information from 1 cell in a database for example. This is a silly. big and amazing. sentence of awesomeness.
Is there a way that i can split the read in php so it comes out in a new line for every full stop.
<?php
$query = "SELECT * from testview";
$res = mysqli_query($con, $query);
$row s =$res->num_rows;
$i = 1;
while($result = mysqli_fetch_array($res)) {
?>
<?php echo $result['QuestionText'];?>
<?php } ?>
I want to split the echo
Going off your examples, lets say
$result['QuestionText'] = "Hello welcome to my supercool website. do you have any time to play a game. no well okay thanks for coming anyway.";
And you want to split this up onto different line by sentence.
Quick way is to replace the periods with a <br>\n
echo str_replace('.', '.<br/>\n', $result['QuestionText']);
A more desirable way (IMO) would be to split the text into an array by the periods.
$aryQText = explode('.', $result['QuestionText']);
Now $aryQText is an array with three elements, each is a sentence from your text. Then you can just loop through the element like so.
foreach($aryQText as $sentence){
echo "$sentence.<br/>\n";
}
This way is much more flexible.
Im learning JQuery and php.
Is it possible to store multiple video links within variables and get php to echo one at random??
My goal is to control my own video ads on my site and I thought this would be a good idea but I dont have a clue where I should look online.
Here is what I was thinking
<?php
$advert1 = 'MyVIDEO1.mp4';
$advert2 = 'MyVideo2.mp4';
$advert3 = 'MyVideo3.mp4';
I want a code that would go here and say: randomly select one of these vars.
echo "At random one of the vars";
?>
I hope Im making sense. help?
You could make an array, like this:
$advert[] = array();
$advert[1] = 'MyVIDEO1.mp4';
$advert[2] = 'MyVIDEO2.mp4';
$advert[3] = 'MyVIDEO3.mp4';
$chosen_one = rand(1,count($advert));
echo $advert[$chosen_one];
You can also use array_rand() instead of shuffle():
<?php
$videos = array("MyVIDEO1.mp4", "MyVideo2.mp4", "MyVideo3.mp4");
echo $videos[array_rand($videos)];
?>
For embedding the video in the right way, have a look at http://www.w3schools.com/html/html_videos.asp
For a start, PHP itself won't exactly play a video. You could use it to echo out one of the URL's to a video. So, bear in mind there is a lot more to do than just select a video.
To answer your question in the code I'd recommend the following:
Try looking up PHP arrays. You want to keep all the videos in an array.
Next up, you'll want to shuffle that array. Then select the first element.
$videos = array("MyVIDEO1.mp4", "MyVideo2.mp4", "MyVideo3.mp4");
shuffle($videos);
echo $videos[0];
This is a simple solution
Place your adverts in an array
<?php
$adverts = array("advert1" => "MyVIDEO1.mp4",
"advert2" => "MyVIDEO2.mp4",
"advert3" => "MyVIDEO3.mp4");
$count = count($adverts);
$rand_advert = rand(1, $count);
echo $adverts['advert'.$rand_advert];
?>
Or alternatively if you don't want to have keys and just put the values in the array straight away
<?php
$adverts = array("MyVIDEO1.mp4",
"MyVIDEO2.mp4",
"MyVIDEO3.mp4");
shuffle($adverts);
echo $adverts[0];
?>
You have need all value in a array then find index randomly and show it.
<?php
$advert = array(
'MyVIDEO1.mp4',
'MyVideo2.mp4',
'MyVideo3.mp4'
);
$total_video = count($advert);
$total_video--; //array index starting from 0 so decrease 1
$random_index = rand(0, $total_video); //array index 0 to 2
$video_to_play = $advert[$random_index];
echo $video_to_play;
?>
I'm pretty new to PHP so please bear with me for this one.
I have an array with emoticons, and I want to replace the emoticon text with the correct image, all within a for loop. So I'm trying to take my text variable and do a str_replace, but I'm not sure exactly how to display the text after the emoticons have been changed.
Here is my code:
$content = ":D Here is a sample sentence for this example :)";
$emotes = array(
[":)","<img class='emoticon' src='smile.png'>"],
[":D","<img class='emoticon' src='grin.png'>"],
);
for($i=0;$i<count($emotes);$i++) {
$contentWithEmotes = str_replace($emotes[$i][0], $emotes[$i][1], $content);
}
print $contentWithEmotes;
The problem this this is that it only displays the last image from the array, when I want it to display both of them.
How should I go about displaying the content with the correct image?
Thanks in advance for any help.
Restructure your array like this:
$emotes = [
":)"=>"<img class='emoticon' src='smile.png' />",
":D"=>"<img class='emoticon' src=grin.png' />"
];
Then use strtr:
$contentWithEmotes = strtr($content,$emotes);
Each time through the loop you need to process the result of the previous time, not the original content.
$contentWithEmotes = $content;
foreach ($emotes as $emote) {
$contentWithEmotes = str_replace($emote[0], $emote[1], $contentWithEmotes);
}
However, the strtr() solution is better.
I am working on a WordPress site where the client has uploaded thousands of photos to Flickr and now want me to move them all back into WordPress and associate them with there proper posts.
Even though there are thousands of images, there is really only about 50 unique images, all the other versions are the same image but uploaded to a different location on Flickr or a slightly different size or name.
In helping me track down all the unique images, based on a list like below, that part I have highlighted, I need to pull every record into a PHP array, the catch is the part I have highlighted, is what I want to make sure is UNIQUE among all records in the array.
Any help in taking an existing PHP ARRAy that has every record and making the array only show unique values based on that part of the Value string?
Is this a Regular Expressions use case?
If it used Regex or similar I think a pattern it could look for is /4485116555_ / followed by 10 digits and then followed up with a _
Appreciate any help in getting me 1 step closer to my goal, this is just 1 piece of the big puzzle.
http://farm5.static.flickr.com/4042/4485116555_19cc0eaa85.jpg
http://farm3.static.flickr.com/2703/4485767454_77476dbdd0.jpg
http://farm5.static.flickr.com/4008/4485116637_ff085b0ab2.jpg
http://farm5.static.flickr.com/4002/4485766896_af83d349c4.jpg
http://farm5.static.flickr.com/4037/4485766950_50d5739344.jpg
http://farm3.static.flickr.com/2785/4485116905_1fa0e2ea6c.jpg
http://farm5.static.flickr.com/4052/4704387613_77542dac2e.jpg
http://farm3.static.flickr.com/2734/4485767622_7b04c3bd3e.jpg
http://farm5.static.flickr.com/4037/4485767292_1a37fe6c57.jpg
http://farm5.static.flickr.com/4038/4485116955_f9c47672c3.jpg
http://farm5.static.flickr.com/4051/4485115681_6d7419a00b.jpg
http://farm3.static.flickr.com/2753/4485116095_30161a56bb.jpg
http://farm5.static.flickr.com/4123/4831194968_3977dff9dc.jpg
http://farm5.static.flickr.com/4054/4538941056_cda5a8242d.jpg
http://farm3.static.flickr.com/2091/4515081466_43cd1624ce.jpg
http://farm3.static.flickr.com/2684/4485766664_3bb9dd9c80_m.jpg
http://farm5.static.flickr.com/4010/4485115557_a38aac0e1f.jpg
http://farm5.static.flickr.com/4055/4485115633_19e6e92276.jpg
http://farm5.static.flickr.com/4045/4485766710_08691e99ed_m.jpg
http://farm5.static.flickr.com/4024/4485115521_9ab2a33d53_m.jpg
http://farm5.static.flickr.com/4048/4505577820_81ce080f2a_t.jpg
http://farm6.static.flickr.com/5294/5389182894_920a54ce97_m.jpg
http://farm5.static.flickr.com/4152/5073487038_5bdb9e3cbc_t.jpg
http://farm5.static.flickr.com/4024/4485115401_67a8957509_m.jpg
http://farm5.static.flickr.com/4062/4485766842_2209843592_m.jpg
$ids = array(); // Where we will keep our unique list of IDs
$lines = array(/* your list of URLs here */);
foreach ($lines as $line) {
preg_match(
'|^http://[A-Za-z0-9\\.]+/[0-9]+/([0-9]+)_[a-f0-9]+.*\\.jpg$|',
'http://farm5.static.flickr.com/4054/4538941056_cda5a8242d.jpg',
$matches
);
echo $matches[1]; // 4538941056
$ids[] = $matches[1]; // Push that into the IDs array
}
$ids = array_unique($ids);
print_r($ids);
Use this code to get your ID Portion
$url = 'Your image url';
$path = parse_url($url, PHP_URL_PATH);
$pathFragments = explode('/', $path);
$end = end($pathFragments);
$id = substr($end,0,9);
And then run array_unique() to get the unique values.