Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9055343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:55:59+00:00 2026-06-16T13:55:59+00:00

The following snippet of PHP code creates $desc alright, but I like it to

  • 0

The following snippet of PHP code creates $desc alright, but I like it to introduce two (2) blank spaces between every dpItemFeatureList found as it goes through its iteration.

I can’t seem to garner exactly what or where to add a snippet to do this?

function get_description($asin){
$url = 'http://www.amazon.com/gp/aw/d/' . $asin . '?d=f&pd=1';
$data = request_data($url);
$desc = '';

if ($data) {
    $dom = new DOMDocument();
    @$dom->loadHTML($data);
    $xpath = new DOMXPath($dom);
    if (preg_match('#dpItemFeaturesList#',$data)){
        $k = $xpath->query('//ul[@class="dpItemFeaturesList"]');
        foreach ($k as $c => $tot) {
            $desc .= $tot->nodeValue;
        }
    }
}
return $desc;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:56:00+00:00Added an answer on June 16, 2026 at 1:56 pm

    Looking at the code you have shared here and consequently having a look at the data that you are processing (a sample of which I have pasted here) you actually want to collect the text within the <li> child elements of the <ul class="dpItemFeaturesList"> node.

    In your original code snippet your XPath is as follows:

    '//ul[@class="dpItemFeaturesList"]'
    

    This will only select the <ul> element and not the child elements. Consequently when you try to do a $tot->nodeValue it will concatenate all the text within all it’s child nodes without spaces (ah ha, the real reason why you want spaces in the first place).

    To fix this we should do two things:

    1. Select the <li> nodes within the appropriate node. Change the XPath to //ul[@class="dpItemFeaturesList"]/li.
    2. In the foreach loop concatenate 2 non-breakable spaces &nbsp;&nbsp; (because this is HTML) to the $desc variable.

    Here $c is the array index.

    function get_description($asin){
        $url = 'http://www.amazon.com/gp/aw/d/' . $asin . '?d=f&pd=1';
        $data = request_data($url);
        $desc = '';
    
        if ($data) {
            $dom = new DOMDocument();
            @$dom->loadHTML($data);
            $xpath = new DOMXPath($dom);
            if (preg_match('#dpItemFeaturesList#',$data)){
                $k = $xpath->query('//ul[@class="dpItemFeaturesList"]/li');
                foreach ($k as $c => $tot) {
                    if ($c > 0) {
                        $desc .= "&nbsp;&nbsp;";
                    }
                    $desc .= $tot->nodeValue;
                }
            }
        }
        return $desc;
    }
    

    We check for $c > 0 so that you will not get extra spaces after the last node in the loop.

    P.S.: Unrelated to your original question. The code for which you shared a link has an undefined variable $timestamp in $date = date("format", $timestamp); on line 116.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw the following code snippet: <?php if(!empty($_POST)): // case I: what is the
delete_define.php has the following code snippet: <?php session_start(); ?> <form action=delete_now.php target=upload_target onsubmit=return my_func_1();>
I'm trying to convert the following code snippet from PHP to C# or VB.NET
The following PHP code snippet uses GD to resize a browser-uploaded PNG to 128x128.
I have PHP code snippet the following: if (!($result = mysql_query($query, $link))) { die(Invalid
All I've got is the following little snippet of code: <select size="1" name="EventHour<?php echo
Good day. I am having the following code snippet in init.php. <?php include_once(config.php); include_once(__SITE_PATH
using the stumbleupon badge code for a url like http://www.test.com/¿cómo_se_dice with the following snippet
I have a line of PHP code that does the following: $xml = <xml><request>...[snipped
I wrote the following snippet in order to handle errors. (Page.php gets included in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.