I am tryng to get the ,,sold" info from eBay listing- https://www.ebay.co.uk/itm/Box-With-Tail-Pipe-Rear-Back-Silencer-Fits-Citroen-C2-C3-I-C3-Pluriel-GCN499/254292997729?hash=item3b350b3661:g:clEAAOSwnhldLB4J.
Here is the screenshot:
As you can see I want to get ,1 sold" text on the upper right corner of the screen. I am using the class ,,vi-txt-underline" to get it, however it is not working. Does anyone know how this can be done, using other attribute or something different? Here is the code:
$sold = $html->find(".vi-text-underline", 0);
if($sold != null){
$item['sold'] = $sold->find("a", 0)->plaintext;
}else{
$item['sold'] = '';
["tag"]=>
string(4) "text"
["attr"]=>
array(0) {
}
["children"]=>
array(0) {
}
["nodes"]=>
array(0) {
}
["parent"]=>
*RECURSION*
["_"]=>
array(1) {
[4]=>
string(6) "1 sold"
The above is part of the debugged $sold variable.
I am using an array $item[] because I am also searching for more info before this part of the code.
get page contents
$url = "https://www.ebay.co.uk/itm/Box-With-Tail-Pipe-Rear-Back-Silencer-Fits-Citroen-C2-C3-I-C3-Pluriel-GCN499/254292997729?hash=item3b350b3661:g:clEAAOSwnhldLB4J";
$content = file_get_contents($url);
find what you want
echo strpos($content,'1 sold');
Related
Im just trying to get a page count from a local pdf file.
I converted the pdf to string and tried getting the page number from it.
I tried using regular expression. But im not able to perfect it.
So please help.
The below is the string text
object(setasign\Fpdi\PdfParser\Type\PdfDictionary)#2728 (1) { ["value"]=>
array(3) { ["Size"]=> object(setasign\Fpdi\PdfParser\Type\PdfNumeric)#2726
(1) { ["value"]=> int(3028) } ["Root"]=>
object(setasign\Fpdi\PdfParser\Type\PdfIndirectObjectReference)#2725 (2) {
["generationNumber"]=> int(0) ["value"]=> int(3027) } ["Info"]=>
object(setasign\Fpdi\PdfParser\Type\PdfIndirectObjectReference)#2731 (2) {
["generationNumber"]=> int(0) ["value"]=> int(3026) } } } } } }
["objects":protected]=> array(0) { } }
["pageCount":protected]=> int(96)
["pages":protected]=> array(0) { } } } ["currentReaderId":protected]=>
string(71)
"C:\xampp\files\journals\2\articles\13\submission\mergedpdf\allFiles.pdf"
["importedPages":protected]=> array(0) { } ["objectMap":protected]=>
array(0) { } ["objectsToCopy":protected]=> array(1) { ["C:\xampp\files\journals\2\articles\13\submission\mergedpdf\allFiles.pdf"]=>
array(0) { } } }
I need to get the pageCount displayed in blockquote using regularexpression.
The regular expression code below:
ob_start();
var_dump($pdf);//this was an object so i converted it to string for pattern matching.
$result = ob_get_clean();//shows the result in string.
$subject = "pageCount";
$pattern = '/^pageCount/';//pattern to match to get page count
preg_match($pattern, substr($subject,20), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
you could use method provided by the library, like:
$filename = 'some-file.pdf';
require_once('library/SetaPDF/Autoload.php');
// or if you use composer require_once('vendor/autoload.php');
$document = SetaPDF_Core_Document::loadByFilename($filename);
$pages = $document->getCatalog()->getPages();
$pageCount = $pages->count();
echo $pageCount;
I would rather loop through the object given instead of using regex for getting values out of it.
But if this is really what you need here is your regex: \["pageCount":protected\]=> int\(\d*\)
You can test it here:
https://regex101.com/r/RyGMwb/2
Seems really easy, but I can't seem to figure it out...
I have a simple line that gets mysql results through wordpress like this:
$sql_results = $wpdb->get_results($sql_phrase);
Then I parse it as JSON and echo it: json_encode($sql_results);
However, I want to add other data before I parse it as JSON. But I'm not sure how.
$sql_results basically gets me a list of post ID's, title and category.
It looks like this in var_dump (this is just the first row):
array(1)
{
[0]=> object(stdClass)#2737 (7)
{
["ID"]=> string(4) "2700"
["post_title"]=> string(18) "The compact helmet"
["category"]=> string(5) "Other"
}
}
Now to start with something easy, I'd like all associative arrays inside the object to have the extra key-value. I tried the following but got an error:
500 Internal error.
foreach($sql_search as $key => $value)
{
$value['pic_img'] = "test";
$sql_search[$key]=$value;
}
$result=$sql_search;
$sql_results = array(1)
{
[0]=> object(stdClass)#2737 (7)
{
["ID"]=> string(4) "2700"
["post_title"]=> string(18) "The compact helmet"
["category"]=> string(5) "Other"
}
}
foreach($sql_results as $key=>$value)
{
$value->solution = 'good';
$sql_results[$key]=$value;
}
$result=$sql_results;
var_dump($result);
$test = array ( array("ID"=>"35", "name"=>"Peter", "age"=>"43"),
array("ID"=>"34", "name"=>"James", "age"=>"19"), array("ID"=>"31", "name"=>"Joe", "age"=>"40") );
foreach($test as $key=>$value)
{
$value['solution'] = 'good';
$test[$key]=$value;
}
$result=$test;
var_dump($result);
I am using the PHP Simple HTML DOM Parser to scrape some results from a page.
At the moment I am having a problem with the function as it is not returning the array "$result".
Any help would be greatly appreciated :)
The result of the array:
array(1) { [0]=> array(6) { ["itemid"]=> string(6) "123456" ["title"]=> string(21) "XXX Prod1" ["unit"]=> string(6) "500ml " ["price"]=> string(4) "2.59" } [1]=> array(6) { ["itemid"]=> string(6) "123457" ["title"]=> string(27) "XXX Prod2" ["unit"]=> string(6) "500ml " ["price"]=> string(5) "10.49" }
Code in question:
function parseItems($html) {
foreach($html->find('div.product-stamp-inner') as $content) { //Finds each individual product on page and extracts its details and stores it into its own array
$detail['itemid'] = filter_var($content->find('a.product-title-link', 0)->href, FILTER_SANITIZE_NUMBER_FLOAT);
$detail['title'] = $content->find('span.title', 0)->plaintext;
$detail['unit'] = $content->find('span.unit-size', 0)->plaintext;
$detail['price'] = filter_var($content->find('span.price', 0)->plaintext, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
$result[] = $detail; //Puts all individual product arrays into one large array
}
//var_dump($result); --Testing purposes
return $result;
}
I guess what you have a piece of code like so
parseItems($html);
When it should be the following because it is returning a variable and needs a variable to hold its returning result
$retval = parseItems($html);
Here's what I am trying to achieve.
I am importing an xml file of products (the product tag has an id attribute:
as an example.
There is 1 tag (category) inside the product tag that I am trying to retrieve but I only want one instance of it. So I created a flag to help distinguish when the product id changes.
My code looks like this
<?php
$flag='start';
foreach($xml->product as $product)
{
$attrs = $product->attributes();
echo "$attrs ($flag)"; // just used for testing results
if ($flag != $attrs) {
echo "| <a href='xmltest.php?menuitem=$attrs'>".$product->category." </a><br>";
$flag=$attrs;
}
}
?>
What should happen on the first run is the flag doesn't match the attrs, the link is echoed, the flag now matches the attrs.
If I have 5 product tags with ID's of 1,1,2,2,2, the code should echo the link twice (first when $flag=start while $attrs=1, and when $flag=1 while $attrs=2)...
Instead, it echoes it all 5 times, basically ignoring the if statement.
I can't see where I am going wrong with the if statement. Can anyone help?
Update Thanks Showerhead, I have been trying your suggestions and I am a bit closer than before.
I did the var_dump and the result I am seeing looks like this
flag var_dump result:string(5) "start"
attrs var_dump result: object(SimpleXMLElement)#6 (1) { ["#attributes"]=> array(1) {
["id"]=> string(1) "0" } } 0 (0)
flag var_dump result:object(SimpleXMLElement)#6 (1) { ["#attributes"]=> array(1) {
["id"]=> string(1) "0" } }
attrs var_dump result: object(SimpleXMLElement)#5 (1) { ["#attributes"]=> array(1) {
["id"]=> string(1) "0" } } 0 (0)
flag var_dump result:object(SimpleXMLElement)#5 (1) { ["#attributes"]=> array(1) {
["id"]=> string(1) "0" } }
attrs var_dump result: object(SimpleXMLElement)#4 (1) { ["#attributes"]=> array(1) {
["id"]=> string(1) "0" } } 1 (0)
flag var_dump result:object(SimpleXMLElement)#4 (1) { ["#attributes"]=> array(1) {
["id"]=> string(1) "0" } }
attrs var_dump result: object(SimpleXMLElement)#8 (1) { ["#attributes"]=> array(1) {
["id"]=> string(1) "1" } } 1 (1)
flag var_dump result:object(SimpleXMLElement)#8 (1) { ["#attributes"]=> array(1) {
["id"]=> string(1) "1" } }
I understand now how the $flag var starts off as a string and then adopts the value of the array.
I tried if( ! is_array($flag) && $flag != $attrs) { but the results were the same.
Is there another way I can get the values to be similar for comparison?
Thanks again,
UPDATE
a simple explode helped solve it. Here is the code that is working for me.
<?php
$flag='start';
foreach($xml->product as $product)
{
$category = $product->category;
$attrsvar = $product->attributes();
$attrs = explode(" ", $attrsvar);
if( ! is_array($flag) && $flag != $attrs[0]) {
echo " <a href='xmltest.php?menuitem=".$attrs[0]."'>".$category." </a> | ";
$flag=$attrs[0];
}
}
?>
You are going to want to make sure it is not an array first.
if( ! is_array($flag) && $flag != $attrs) { etc... }
There that should be it, sorry if you were reading first post before I edited it. I completely misread your problem. Now the reason this should work is because you were originally trying to compare an array to a string, which was probably throwing silent errors.
Edit: If its still not working try var_dump() to make sure both variables are the same type when they are being compared.
Update: You know what, we are both over thinking this. I will assume you are doing something with this XML other than just getting categories, otherwise you can look into the much simpler XPATH solution below. If you are doing something more with this XML you could just do the following.
$products = array();
foreach($xml->product as $product) {
$id = $product['id'];
$category = $product->category;
if( ! in_array($id, $products)) {
$products[$id] = $category;
echo "| <a href='xmltest.php?menuitem=$attrs'>".$category." </a><br>";
}
}
XPATH for if you just want to work with categories. Let me know if my XPATH is not quite right, I'm not too good with it under normal circumstances, I'd hate to think how badly I could mangle it under sleep depravation.
$categories = $xml->xpath('//product/category');
$categories = array_unique($categories);
foreach($categories as $category) {
echo "| <a href='xmltest.php?menuitem=$attrs'>".$category." </a><br>";
}
This is how the array comes out
array(3) { [0]=> string(3) "174" [1]=> object(SimpleXMLElement)#5 (1) { [0]=> string(2) "41" } [2]=> object(SimpleXMLElement)#4 (1) { [0]=> string(2) "21" } }
I'm using this code here that generates the array.
while($row = mysql_fetch_assoc($results)){
$values[] = $row['id'];
$dom = simplexml_load_file('../data/'.$row['id'].'.xml');
foreach($dom->children() as $child)
{
$values[] = $child->views;
}
}
var_dump($values);
The xml file looks like this
<?xml version="1.0"?>
<website site_id="174" user_id="26">
<view day="23" month="10" year="11">
<views>31</views>
</view>
<view day="23" month="12" year="11">
<views>21</views>
</view>
</website>
I need to get the value of the Views into an array, but I keep getting these annoying
object(SimpleXMLElement)#5 things in the array. Also this string(3) . How do I get rid of those.
Thank you
Try to change
$values[] = $child->views;
with
$values[] = (string)$child->views;
How do I get rid of those
If you don't need to see the type of the variable - just don't use var_dump(), but print_r() instead
To explain (string): This is called 'typecasting'. Also works with other types such as (int), (bool), etc.