php parse content for input->name value - php

I'm trying to parse html content, for the value stored in the name tag.
My current try looks like the following:
function getAuthToken(){
$html = file_get_contents('url');
$values = array();
foreach($html->find('input') as $element)
{
$values[$element->name] = $element->value;
}
print_r($values);
}
What am I doing wrong?

$html in your code is not an object its a string (the results of file_get_contents).
$object->name is a class object
$array[key] is an array element
Neither of these in your code will do what you want to achieve with the result of file_get_contents because your $html var is a string, you need to do some string parsing to get the desired results.
You can check that with:
echo gettype($html);
You can also just echo out $html to find out what the string looks like to give you a better idea of what you are working with, for instance, is there a common character that you can explode the sting with an so getting an array to work with within your foreach.
example:
$newarray = explode('&', $html);
foreach ($newarray as $key => $value) {
//do your thing
}

Related

PHP: String in json parse

I am trying to parse a json. I am running this in a foreach loop and if I do the following it works:
$places = array('restaurant', 'store', 'etc')
foreach ($this->placesCachingTypes as $places) {
$places_location_lat = $json_decoded->json[0]->restaurant[0]->geometry->location->lat;
$places_location_lng = $json_decoded->json[0]->restaurant[0]->geometry->location->lng;
}
However, when I do the following, i.e. I change restaurant to $places (I need to do this since I have an array of different places and I want to parse all of them in a foreach loop) it doesn't work.
foreach ($this->placesCachingTypes as $places) {
$places_location_lat = $json_decoded->json[0]->$places[0]->geometry->location->lat;
$places_location_lng = $json_decoded->json[0]->$places[0]->geometry->location->lng;
}
Solution is changing $places to {$places}[0]
The $places array contains keywords, such as restaurant or store. So the [0] is referring to the first one in the json which is why it's needed.
Why do you have this in the first loop:
json[0]->restaurant[0]
json[0]->$restaurant[0]
But then in the next you have:
json[0]->$places[0]
json[0]->$places[0]
Perhaps you are parsing the JSON incorrectly and it should be:
foreach ($this->placesCachingTypes as $places) {
$places_location_lat = $json_decoded->json[0]->places[0]->geometry->location->lat;
$places_location_lng = $json_decoded->json[0]->places[0]->geometry->location->lng;
}
And then in the first loop, you should do a similar edit to get rid of $restaurant[0]:
foreach ($this->placesCachingTypes as $places) {
$places_location_lat = $json_decoded->json[0]->restaurant[0]->geometry->location->lat;
$places_location_lng = $json_decoded->json[0]->restaurant[0]->geometry->location->lng;
}
Then again, unclear on what value $places has when you loop via foreach ($this->placesCachingTypes as $places) {. It does’t make sense what you would be looping through with the value of $places. And perhaps assigning that $places in the loop object of $json_decoded->json[0]-> is the source of your issues? Need more info from you to confirm this.

Extract only if the script tag's src contains a particular word in PHP

I am learning PHP. I have to extract all the script tags of a particluar $url whose src contain a particular word say 'abc'.
I have tried this:
$domd = new DOMDocument();
#$domd->loadHTML(#file_get_contents($url));
$data = array();
$items = $domd->getElementsByTagName('script');
foreach($items as $item) {
if($item->hasAttribute('src')){
$data[] = array(
'src' => $item->getAttribute('src')
);
}
}
print_r($data);
echo "\n";
The above code gives me the list of all the script tag's src's present in the $url.
But how should i check if a src in a script tag contains a word 'abc' ?
If you need to check each value in the array, to see if it contains 'abc', try a foreach() loop with an if statement using strpos() to see if the value contains 'abc', and then do something with it.
Something like this should do the trick:
foreach( $data as $key=>$value ) {
if( strpos( $value['src'],'abc' ) !== false ) {
//do something with it here
}
}
Just edited this. Call the ['src'] element of the subArray and use that in strpos(). Alternately, you could change your line that builds the $data array to this, since you only have one element in each subArray:
if($item->hasAttribute('src')){
$data[] = $item->getAttribute('src');
}

Printing contents of a nested array

I am trying to print the contents of a nested array, but it simply returns "Array" as a string yet will not iterate with a foreach loop. The arrays are coming from a mongodb find(). The dataset looks like this:
User
Post_Title
Post_Content[content1,content2,content3]
I am trying to get at the content1,2,3.
My current code looks like this:
$results = $collection->find($query);
foreach ($results as $doc)
{
echo $doc['title']; //this works
$content[] = $doc['content'];
print_r($content); //this prints "Array ( [0] => Array )"
foreach ($content as $item)
{
echo $item;
}
}
All this code does is print the Title, followed by Array ( [0] => Array ), followed by Array.
I feel quite stupid to not figure out something that seems so basic. Most posts on stack overflow refer to multidimensional associative arrays - in this case, the top level array is associative, but the content array is indexed.
I have also tried
foreach ($doc['content'] as $item)
But that gives
Warning: Invalid argument supplied for foreach()
I even tried iterating over the returned array again using a nested foreach, like the following:
foreach ($results as $doc)
{
echo $doc['title']; //this works
$content[] = $doc['content'];
print_r($content); //this prints "Array ( [0] => Array )"
foreach ($content as $item)
{
foreach ($item as $next_item)
{
echo $next_item;
}
echo $item;
}
}
The second foreach failed with an invalid argument..
Any thoughts would be greatly appreciated.
edit: perhaps it has something to do with how I am inserting the data to the DB. That code looks like this
$title = $_POST['list_title'];
$content[] = $_POST['list_content1'];
$content[] = $_POST['list_content2'];
$content[] = $_POST['list_content3'];
$object = new Creation();
$object->owner = "$username";
$object->title = "$title";
$object->content = "$content";
$object->insert();
Is this not the proper way to add an array as a property to a class?
The issue is some of array content is an array while other parts are strings. You should test for the sub content $doc being an array using is_array(). If it is, loop through the $doc like you would any other array, and if it isn't, you can echo the content (may need to test the content type before doing this if you're uncertain as to what content it can be)
The problem is in the code that you are saving data. You should replace the following line
$object->content = "$content";
with
$object->content = $content;

url_to_absolute query

right, i am building a web crawler and there is a section of my code which translates to absolute urls instead of /macbookpro/ to http://www.apple.com/macbookpro. but when i echo my code, it only prints one result, which is the first link it sees why. Do i have to create an array, because when i did, i echoed the array and listed was the word 'Array'
<?php
require_once('simplehtmldom_1_5/simple_html_dom.php');
require_once('url_to_absolute/url_to_absolute.php');
$URL = 'http://www.theqlick.com'; // change it for urls to grab
// grabs the urls from URL
$file = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
$links = url_to_absolute($URL, $theelement->href);
}
echo $links;
?>
var_dump your array, it gives you a text representation of your objects. It will show you the array and it's elements. Echo is more for just outputting strings. you could loop your array and echo each element, but if you just want to see it, var_dump is the answer.
http://www.php.net/manual/en/function.var-dump.php
If you are trying to build an array in $links You need to do
$links[] = url_to_absolute($URL, $theelement->href);
Right now, you are overriding the value of $links with each loop iteration.
You should also decalre $links = array(); somewhere before your foreach loop.
<?php
require_once('simplehtmldom_1_5/simple_html_dom.php');
require_once('url_to_absolute/url_to_absolute.php');
$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab
// grabs the urls from URL
$file = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
$links[] = url_to_absolute($URL, $theelement->href);
}
print_r($links);
So you'll need to init the array, add to it with [] and finally use something suitable to actually print it out, such as print_r.

basic json decode question

good evening. ingore all other elements, I want get first image in each foreach. how to?
$data = json_decode($json,true);
foreach ($data['data'][0] as $image) {
echo '<img src="'.$image['images'][0]['source'][0].'" />';
}
json tree is too large, over the letters paste limit.
Oops! Your question couldn't be submitted because:
body is limited to 30000 characters; you entered 68494
so here is the url u can get the json tree https://graph.facebook.com/5550296508/photos
and u can paste in http://jsonlint.com/ look the struction well. Thanx.
With json_decode every {} (object) will become an object(stdClass) and every [] (array) will become an array. So:
$data->data[0]->images[0]->source
is what you need, to reach the first image source.
Edit: since the second parameter of json_decode is true, it will become an associative array, and it will be like:
$data['data'][0]['images'][0]['source']
To get all the images:
$images = array();
foreach ($data['data'] as $d)
{
foreach ($d['images'] as $i)
{
$images[] = $i['source'];
}
}

Categories