Monday, September 23, 2013

Not showing SVN Logs on Zend Studio 10

When i recently moved to Zend Studio 10 from 8, i see wired issue on displaying the SVN Logs. Whenever i commit my changes, Zend Studio 8, used to disply the logs on the SVN Console, but this was not happening in Zend Studio 10. How to resolve that?

You have to enable Console Output manually.

In the menu bar select Window->Preference.

In the Preference selecte Team -> SVN -> Console.

This trigger the Console Popup screen, as shown below. In that you will see options under Show console automatically, you have to select the option On Ouput which will show the SVN logs on the console.



Happy Programming...!!!

Saturday, September 14, 2013

Get Innerhtml by Parsing html DOM with DOMDocument

The domdocument class of PHP is a very handy one which can be used for a number of tasks like parsing xml, html and creating xml. In this tutorial we are going to see how to use this class to parse html content, and get a Innerhtml of a specific div element.

Below is the sample html code, from which we are going to get the innerhtml of the div tag with id=test2.
 <html>  
 <title>Test Content</title>  
 <body>  
 <div id="test1" class="container1">  
   Test content inside div id test1  
 </div>  
 <div id="test2" class="container2">  
   <p>Test content inside div id test2</p>  
 </div>  
 </body>  
 </html>  
Lets construct DOMDocument object and load the html content into it.
 $dom = new DOMDocument('1.0');;   
 $siteUrl = "http://*******";  
 //Fetch the filec content from the url  
 $file = file_get_contents($siteUrl);  
 // load the html into the object  
 $dom->loadHTML($file);   
 // discard white space  
 $dom->preserveWhiteSpace = false;  
Now the $dom object has loaded with the html content, which and can be used to extract contents from the whole html structure. DOMDocument has enough functions to make our work easier to parse and extract the content as needed, some of the common function which will be used frequently are getElementsByTagName and getElementById.

Lets how we can extract the div based on the id. To fetch the div based on id, we need to use the getElementById.

//get element by id
$test1div = $dom->getElementById('test1');

This will return the DOMNode, through which can fetch the text content my accessing $test1div->textcontent; that's simple. But if you want to fetch the innerhtml, you need to iterate your node, and add up all the child elements and display it. Below we have created a new method, which accepts the DOMNode
and iterates through all the childNodes within that, accumulates the data, and saves it as XML string and returns it.

 function getInnerHtml( $node )   
 {  
   $innerHTML= '';  
   $children = $node->childNodes;     
   foreach ($children as $child)  
   {  
     $innerHTML .= $child->ownerDocument->saveXML( $child );  
   }     
   return $innerHTML;  
 }  

That's it, you will get the Innerhtml of the DIV, with the all the html tags inside the div as it is.

Happy Programming...!!!