I was wondering on how to get a specific string out of an array within this document:
https://gravatar.com/205e460b479e2e5b48aec07710c08d50.php
I was working with the example given on the Gravatar page and started to get used to the following line:
$profile['entry'][0]['preferredUsername'];
The complete document (of mine) now looks like this so far:
<?php
error_reporting(error_reporting() & ~E_NOTICE);
$str = file_get_contents('https://www.gravatar.com/'.$_GET['hash'].'.php');
$profile = unserialize($str);
if (is_array($profile) && isset($profile['entry']))
echo $profile['entry'][0]['name'];
echo $profile['entry'][0]['preferredUsername'];
?>
My problem is how to figure out how to get the name and last name using this line:
echo $profile['entry'][0]['name'];
All I get by typing this single line is "Array".
$profile['entry'][0]['name'] is indeed an array. If you want the formatted name you should access $profile['entry'][0]['name']['formatted']
Related
Here i have single line of variable value. You can see here below in php source code
<?php
$list = "03343922581 03136011388 03142181356 03471819024 03003973577";
//Please tell me the php code
?>
I want to get output like below
03343922581
03136011388
03142181356
03471819024
03003973577
for show result in html page according to your desire you can use :
$new_list = str_replace(" ","\n",$list);
echo nl2br($new_list);
I am making a price crawler for a project but am running into a bit of an issue. I am using the below code to extract values from an html page:
$content = file_get_contents($_POST['url']);
$resultsArray = array();
$sqlresult = array();
$priceElement = explode( '<div>value I want to extract</div>' , $content );
Now when I use this to get certain elements I only get back
Finance: {{value * value2}}
I want to get the actual value that would be displayed on the screen e.g
Finance: 7.96
The other php methods I have tried are:
curl
file_get_html(using simple_html_dom library)
None of these work either :( Any ideas what I can do?
You just set the <div>value I want to extract</div> as a delimiter, which means PHP looks for it to separate your string to array whenever this occurs.
In the following code we use , character as a delimiter:
<?php
$string = "apple,banana,lemon";
$array = explode(',', $string);
echo $array[1];
?>
The output should be this:
banana
In your example you set the value you want to extract as a delimiter. That's why this happens to you. You'll need to set a delimiter between your string you want to obtain and other string you won't need at the moment.
For example:
<?php
$string = "iDontNeedThis-dontExtractNow-value I want to extract-dontNeedEither";
$priceElement = explode('-', $string);
echo "<div>".$priceElement[2]."</div>";
?>
The code should output this to your HTML page:
<div>value I want to extract</div>
And it will appear on your page like this:
value I want to extract
If you don't need to save the whole array in a variable, you can save the one index of it to variable instead:
$priceElement = explode('-', $string)[2];
echo $priceElement;
This will save only value I want to extract so you won't have to deal with arrays later on.
I have some php code that extracts a web address. The object I have extracted is of the form:
WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults
Now in PHP I have called this object $linkHREF
I want to extract the id element only and put it into an array (I'm bootstrapping this process to get multiple id's)
So the command is:
$detailPagePathArray = explode("id=",$linkHREF); #Array
Now the problem is the output of this includes what comes after the id tag, so the output looks like:
echo $detailPagePathArray[0] = WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&w
echo $detailPagePathArray[1] = bf&page=1&
echo $detailPagePathArray[2] = 16123012&source=searchresults
Now the problem is obvious, where it'd firstly picking up the "id" in the "wid" marker and cutting it there, however the secondary problem is it's also picking up all the material after the actual "id". I'm just interested in picking up "16123012".
Can you please explain how I can modify my explode command to point it to the particular marker I'm interested in?
Thanks.
Use the built-in functions provided for the purpose.
For example:
<?php
$url = 'http://www.example.com?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults';
$qs = parse_url($url);
parse_str($qs['query'], $vars);
$id = $vars['id'];
echo $id; // 16123012
?>
References:
parse_url()
parse_str()
if you are sure that you are getting &id=123456 only once in your object, then below
$linkHREF = "WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults";
$str = current(explode('&',end(explode('&id', $linkHREF,2))));
echo "id" .$str; //output id = 16123012
$tempmoviename = "Battleship";
$omdburl = "http://www.omdbapi.com/?t=" . $tempmoviename;
$imdb_json = file_get_contents($omdburl);
$imdb_info = json_decode($imdb_json,true);
print ($imdb_info[0]->runtime[0]);
I can't get it to print the runtime of the movie. I can get it to print the actual website but not the information I need from the website.
In addition to that if I remove true
json_decode($imdb_json,true);
I get this error.
Fatal error: Cannot use object of type stdClass as array
How do I write this so it grabs the data from an array correctly? I also need to swap spaces and dashes in titles like Black Sheep, Black_Sheep to Black%20Sheep?
Do like this... Since you are converting the JSON to array (by passing true in the json_decode()), You need to access it like an array.
<?php
$tempmoviename = "Battleship";
$omdburl = "http://www.omdbapi.com/?t=" . $tempmoviename;
$imdb_json = file_get_contents($omdburl);
$imdb_info = json_decode($imdb_json,true);
echo $imdb_info['Runtime']; //"prints" 131 min
<?
$Tweet = file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=EdVizenor&count=1");
//$Tweet = explode(",",$Tweet);
/// If I explode and echo the $Tweet(3); i get .... "text":"mY LATEST TWEET"
var_dump(json_decode($Tweet,true));
?>
What I want to do is parse out the array via key. Something like:
echo $Tweet(text); /// but this does not work.
It would be helpful to know what the error is. The problem is that $Tweet(text) would be calling a function contained in $Tweet, since that variable is not a function, but an actual array, you actually just have to
<?php
// your code
$tweets = json_decode($Tweet,true);
echo $tweets[0]['text'];
Next time, be sure to include the error being thrown, it is very helpful for you to read and understand those!