Entrepreneur Geek

Nirav Mehta on life, technology and future

Archive for the ‘PHP’ Category

Fixing bad XML, any recommendations?

without comments

I am using Text_Diff classes of PHP to generate differences between two XML documents. The output is not always valid XML – tag nesting is not always correct. This happens because my source files are XML and have their own tags. When Text_Diff inserts its own <ins> and <del> tags around the changed text, it messes up the tag hierarchy at times.

I am looking for a clean, fast and safe way to fix such invalid XML. Do you have any recommendations?

I have looked at Tidy, it’s PHP library and htmLawed. I liked htmLawed since it’s pure PHP implementation, but don’t know how fast it is compared to Tidy. Moreover, I need an XML cleaner, not necessarily XHTML cleaner. So even if I use these libraries, I will have to strip out the HTML parts from the output.

Do you have any suggestions / recommendations?

Written by Nirav

August 19th, 2009 at 5:12 pm

Posted in PHP

Tagged with , , , ,

Setting up local copy of PHP manual – with smart lookups

with 3 comments

If you are a PHP developer, you probably know that you can type “php.net/substr” to look at documentation for the substr() function. This works for many other keywords as well and is a superb feature. It’s just so convenient, I stopped using my local copy of PHP manual.

But yesterday, I wanted it offline. I downloaded the “many HTML files” version of the documentation, extracted it and set it up on my local Apache. I can now open up http://localhost/phpman/ to read the documentation. (this is how I actually learned PHP 11 years ago!!)

And I wanted the smart lookups – auto complete! I searched around to find if there was a ready .htaccess file that I could use. Or if someone had made something similar already. I did not really find anything solid. Suddenly I realized I can just look up the PHP.net site source code and figure out what they are doing, and replicate it on my local setup. (Yes, PHP.net’s source code is open too!)

Eureka!

So after a few minutes of hacking around, I have my nice little setup that mimics PHP.net’s quick / smart lookup of functions. Typing http://localhost/phpman/substr takes me to http://localhost/phpman/function.substr.html in a split second.

Jai ho!

Let me jump to the code now that you’ve read so much!

Here’s the .htaccess file. And here’s the PHP file – “manual-lookup.php” placed in the phpman folder. The file is an adaptation from php.net website’s source.

Here’s the complete procedure:

  1. Ensure you have Apache / PHP setup locally.
  2. Download the many HTML files version of PHP documentation.
  3. Extract it in a folder called “phpman” in your website root. You should now be able to see it at http://localhost/phpman
  4. Download this file, and save it as .htaccess within the phpman folder.
  5. Download this file, and save it as manual-lookup.php within the phpman folder.
  6. Try http://localhost/phpman/substr – it should work if all went well!
  7. Enjoy!

Hope this helps someone like me ;-)

Written by Nirav

May 29th, 2009 at 2:15 pm

Debugging PHP for the first time in 10 years, and loving it!

with one comment

I have been writing PHP code for 10 years now and have used a variety of ways to debug my code – except the actual runtime debugging! I have used echo statements and log files to debug and solve problems. I knew there is XDebug and Zend Debugger, but never tried them.

debugging-phpToday, I downloaded, setup and used Zend Debugger for the first time. And I loved it! It was awesome to see variable watch, breakpoints and the like. Just loved it!

Why did I use the debugger? I’ve been using Flex Builder myself for last one year heavily and think the debugger is indispensable. I have done some .NET code earlier and love debugger there too. For PHP, it was more like “I don’t need a debugger!” so far.

I dropped that today and embraced a debugger. And I am loving it so far!

Do you use a PHP Debugger?

Written by Nirav

April 21st, 2009 at 7:13 pm

Posted in Flex & Flash, PHP

Tagged with ,

Handling Unicode with PHP

without comments

Unicode characters and webservices always create one or the other problem for me ;-) Working on PlannerX backend was not any different. Spent some good hours fixing Unicode / UTF-8 related issues.

And while I was searching for some solutions, I found an excellent “PHP UTF-8 Cheat Sheet” by Nick Nettleton of DropSend. I highly recommend it if you are going to do anything with PHP and Unicode!

And BTW, don’t use Base64 encoding with UTF-8. It will not work!

Written by Nirav

March 27th, 2009 at 4:32 pm

Posted in PHP, Recommended Reading

Tagged with , ,

WSDL to PHP – Generate PHP code from a WSDL file

with 3 comments

There are many classes available to generate a WSDL file from PHP. But I wanted to create PHP classes out of a WSDL file. SOAP allows passing complex data around and to write a SOAP client, I need to have classes defined in PHP for these complex data. I did not want to manually go through WSDL and create different data types based on SOAP declarations. As a matter of fact, I wanted something that will create a SOAP Server stub for me out of the WSDL (not just a client!)

Now I did not find something that will create a SOAP Server, but I found two implementations that create a SOAP Client out of WSDL. This is still a good start, and I can do some further hacks to create a SOAP server.

Here are these two approaches:

  • WSDLInterpreter – WSDLInterpreter is a library that creates PHP 5 classes based on a WSDL document. It creates a SOAPClient via XSLT transformations. Unlike other wsdl2php solutions, WSDLInterpreter utilizes the WSDL document as its source of information, as opposed to the native SoapClient interpretation of the WSDL document. This allows for proper class inheritance, naming convention correction, and method overloading with proper method signature verification using strict type checking.
  • wsdl2php – wsdl2php is a very simple tool for PHP 5 to generate client code against a WSDL-file. It uses DOM to parse the WSDL and generates PHP code out of it.

Both approaches generate similar output. And are a great start if you want to use a third party web service in your PHP code.

Written by Nirav

January 28th, 2009 at 4:19 pm

Posted in PHP

Tagged with , ,