0

I am not if 'tags' are the right term but i have to get the "data-time" values from this span into an array. How can I use simple html dom to get them?

Here is on span I am trying to get the "data-time" out of.

include('../simpleHtmlDom/simple_html_dom.php');

// Put the Twitters username here
$user = "yadayada";

$html = file_get_html("https://twitter.com/$user");
$ret = $html->find('div[class=ProfileTweet-contents]'); 
$ret = $html->find('p[class=ProfileTweet-text js-tweet-text u-dir]'); 

/// tries to get the time code but does only gets the span
$date = $html->find('span[class=js-short-timestamp js-relative-timestamp]', 0);

$DoesNotWork = $html->find( "data-time", 0 );


echo $ret[1]; // get's a users tweet. 

echo $DoesNotWork; 

The result of the date

<span class="js-short-timestamp js-relative-timestamp"
    data-time="1401528672"
    data-long-form="true">
    15h
  </span>

I would think it is something like this but this code does not work. $html->find( "data-time", 0 );

3
  • You can use regular expression, preg_match("/data-time=.*"/", $input_line, $output_array); Commented Jun 1, 2014 at 4:36
  • simplehtmldom.sourceforge.net/manual.htm#section_access Commented Jun 1, 2014 at 4:40
  • You're talking about attributes, not tags. Commented Jun 6, 2014 at 0:14

4 Answers 4

2

You may try this:

// Include the script
$url = 'https://twitter.com/yourusername';
$html = file_get_html($url);
$dateTimes = array();
foreach ($html->find('div.GridTimeline .js-short-timestamp') as $value) {
    $dateTimes[] = $value->innertext;
}

Result of print_r($dateTimes);:

Array
(
    [0] =>      2h   
    [1] =>      2h   
    [2] =>      2h   
    // Truncated...
    [10] =>      11h   
    [11] =>      May 30   
    [12] =>      May 30   
    [13] =>      May 6   
    // Truncated...
)
Sign up to request clarification or add additional context in comments.

Comments

0

I was able to get the date using this code, tho I think there is a better way. I think it would be best to find a simple dom code that gets the text of the date-time in line

<span class"js-short-timestamp js-relative-timestamp" date-time="89393748474">

but instead I used two "list" php lines as seen below and that worked.

$dateTimes = array();
foreach ($html->find('div.GridTimeline .js-short-timestamp') as $value) {
    $dateTimes[] = $value->outertext;
}
//  These are the lines I get the date-time from.
list($Gone,$Keep) = explode("data-time=\"", $dateTimes[0]);
list($Date,$Gone) = explode("\"", $Keep);

$Date =  date('M d, Y', $Date);

Comments

0

You want to use:

$html->find( "[data-time]", 0 );

Comments

0

In case anyone landing here in 2021, following no 1 google search result:

Unless I misinterpreted your intention, you might achieve what you want using (with simplehtmldom):

$html->find('span[data-time]')->attr[data-time];

The official simplehtmldom documentation fails to mention that. However, https://stackoverflow.com/a/14456823/10050838 is one possible source.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.