Page 1 of 1

parsing output from function

Posted: Mon Dec 22, 2008 3:29 pm
by andyb2
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>&nbsp;(3)</li>

This is the output from a built in wordress function.

Any info would be great

cheers

Re: parsing output from function

Posted: Mon Dec 22, 2008 4:16 pm
by requinix
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?

Re: parsing output from function

Posted: Mon Dec 22, 2008 4:32 pm
by GreenCore
You can use the default domDocument for parsing XML and HTML.
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>&nbsp;(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/>"; 
?>
You can find date-time format's at:
http://www.plus2net.com/php_tutorial/ph ... format.php

Good luck!

Re: parsing output from function

Posted: Tue Dec 23, 2008 8:29 am
by andyb2
cheers greencore,

i'll give that a shot and let you know.