0

I have the following html :

<span class="orig_line">
<a class="original" href="http://nucleify.org/">Nucleify <i class="externalLink icon-circle-arrow-right"></i></a>
&middot;

by <span class="author">Random Person</span>
&middot;
October 1, 2013
</span>

I am using Simple HTML DOM parser class which is available on sourceforge, here is sample code i am using:

$newoutput = str_get_html($htmlCode);
$html  = new simple_html_dom();
$html->load($newoutput);
foreach($html->find('div#titlebar') as $date){
$n['date'] = $date->find('span.orig_line',0)->plaintext);
print $n['date'];
}

As i just want the October 1, 2013 date text from the span (.orig_line) stripping out any further html tags inside it, and just only the text, i cannot find a way around it...

PS: I want to stick to SimpleHTMLDom class only, and no phpQuery or DOMParsers.

Thank you.

1 Answer 1

2

Since "simple_html_dom" is heavily regexp based, you can use regexp to match date in plaintext like so:

require 'simple_html_dom.php';

$htmlCode = '
<div id="titlebar">
<span class="orig_line">
<a class="original" href="http://nucleify.org/">Nucleify <i class="externalLink icon-circle-arrow-right"></i></a>
&middot;

by <span class="author">Random Person</span>
&middot;
October 1, 2013
</span>
</div>';

$html  = new simple_html_dom();
$html->load($htmlCode);

foreach ($html->find('div#titlebar') as $date)
{
  $n = [];
  $plaintext = $date->find('span.orig_line', 0)->plaintext;
  preg_match('#[A-Z][a-z]+ \d{1,2}, \d{4}#is', $plaintext, $matches);
  $n['date'] = $matches[0];
  var_dump($n); # array (size=1) 'date' => string 'October 1, 2013' (length=15)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok that seems good, will this be much better to use? so to eliminate any other text that might come : {code} (?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)+ \d{1,2}, \d{4} {/code}

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.