parsing output from function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andyb2
Forum Newbie
Posts: 7
Joined: Tue Sep 04, 2007 3:16 pm

parsing output from function

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: parsing output from function

Post 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?
GreenCore
Forum Newbie
Posts: 5
Joined: Thu Dec 18, 2008 6:53 am

Re: parsing output from function

Post 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!
andyb2
Forum Newbie
Posts: 7
Joined: Tue Sep 04, 2007 3:16 pm

Re: parsing output from function

Post by andyb2 »

cheers greencore,

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