Hi,
I would like to be able to parse the output below and then print it in a different format. I am having a tough time knowing how to parse the output.
currently the output is:
<li><a href='url' title='May 2008'>May 2008</a> (3)</li>
This is the output from a built in wordress function.
Any info would be great
cheers
parsing output from function
Moderator: General Moderators
Re: parsing output from function
It's input. When you take data and do something with it, it becomes input. The result of the "parsing" is the output.
What are you trying to get out of it? And have you looked for a WP function to get it?
What are you trying to get out of it? And have you looked for a WP function to get it?
Re: parsing output from function
You can use the default domDocument for parsing XML and HTML.
The function below will parse it to an array.
You can find date-time format's at:
http://www.plus2net.com/php_tutorial/ph ... format.php
Good luck!
The function below will parse it to an array.
Code: Select all
<?php
function getParsedTags($tag, $html, $strict=0)
{
// Define new domDocument
$dom = new domDocument;
// Option for using XML or HTML
if($strict==1) {
$dom->loadXML($html);
} else {
$dom->loadHTML($html);
}
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagname($tag);
// Create an Array with Values
$out = array();
foreach ($content as $item)
{
$out[] = $item->nodeValue;
}
return $out;
}
//Your test output
$YourSource = "<li><a href='url' title='May 2008'>[color=#008000][b]May 2008[/b][/color]</a> (3)</li>";
var_dump(getParsedTags("a",$YourSource));
echo "<hr/>";
//Another test
$Example = "<li><a href='nl.wikipedia.org/wiki/Barack_Obama' title='January 20, 2009'>[color=#008000][b]Barack Obama[/b][/color]</a>[color=#008000][b] the new president.[/b][/color]</li>";
var_dump(getParsedTags("li",$Example));
echo "<hr/>";
$dateValue = getParsedTags("a",$YourSource);
$formattedDateA = date("l, jS F Y", strtotime($dateValue[0]));
$formattedDateB = date("m-Y", strtotime($dateValue[0]));
echo $dateValue[0];
echo "<hr/>";
echo $formattedDateA."<br/>";
echo $formattedDateB."<br/>";
?>http://www.plus2net.com/php_tutorial/ph ... format.php
Good luck!
Re: parsing output from function
cheers greencore,
i'll give that a shot and let you know.
i'll give that a shot and let you know.