简体   繁体   中英

How can I return text value of an array instead the function result?

For example if I have an array with one element as follows:

$dom = [file_get_html('https://www.homedepot.com/p/Connecticut-Electric-30-Amp-8-Space-10-Circuits-G2-Manual-Transfer-Switch-Kit-EGS107501G2KIT/100669964', false)];

How can I echo $dom[0]; and have it return JUST the URL (or the text value at minimum) instead of returning the result of the function?

Thank you.

Code:

<?php 
include "db.php";
include "Includes/header.php";
require 'simple_html_dom.php';

$dom = [file_get_html('https://www.homedepot.com/p/Connecticut-Electric-30-Amp-8-Space-10-Circuits-G2-Manual-Transfer-Switch-Kit-EGS107501G2KIT/100669964', false),
        file_get_html('https://www.homedepot.com/p/31180154554', false),
        file_get_html('https://www.homedepot.com/p/Connecticut-Electric-30-Amp-Adapts-to-20-Amp-CESMAD3020/100128920', false)
       ];

$answer = array();
for($i = 0; $i < sizeof($dom); ++$i)
{
if(!$dom[$i]->find('span#ajaxPrice',0))
{
    $formattedUrl = '$dom[$i] - SHOW URL HERE THAT SEARCH WAS ATTEMPTED ON';    
    echo "<tr><td>" . "Not Listed" . "</td><td>" . $formattedUrl . "</td><td>" . "Not Listed" . "</td></tr>";
    continue;
}

$element = $dom[$i]->find('span#ajaxPrice',0);
$title = $dom[$i]->find('h1.product-title__title',0);
$sku = $dom[$i]->find('h2.product_details.modelNo',0);
$formattedSku = str_replace("Model # ", "", $sku);
$titlePlain = $title->plaintext;

if(isset($element->content)) 
{
    $price = $element->content; 
    $priceFloat = floatval($price); 
}

$query = "INSERT INTO homedepot(Date,Title,Price) VALUES (CURDATE(), ?, ?)";
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, "sd", $titlePlain, $priceFloat);
mysqli_stmt_execute($stmt);

echo "<tr><td>" . $formattedSku . "</td><td>" . $titlePlain . "</td><td>" . $price . "</td></tr>";

sleep(1);
}

?>

Just store the URL in the array and then execute the file_get_html when you need it:

$urls = ['https://www.homedepot.com/p/Connecticut-Electric-30-Amp-8-Space-10-Circuits-G2-Manual-Transfer-Switch-Kit-EGS107501G2KIT/100669964',
         'https://www.homedepot.com/p/31180154554',
         'https://www.homedepot.com/p/Connecticut-Electric-30-Amp-Adapts-to-20-Amp-CESMAD3020/100128920'
        ];

foreach($urls as $url)
{
    $dom = file_get_html($url, false);

    if(!$dom->find('span#ajaxPrice', 0))
    {
        echo "<tr><td>" . "Not Listed" . "</td><td>" . $url . "</td><td>" . "Not Listed" . "</td></tr>";
        continue;
    }
    // more code . . .
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM