I am a programmer, an inventor, a creator of ideas and a dreamer. What are you?

Brain Dump: transcoding PHP to Ruby

Posted: January 12th, 2010 | Author: Spoofy | Filed under: Brain Dump | Tags: , , | Comments Off

Today I had to dig into some old code of mine for a project, which tends to happen, however the archival code was in PHP but I needed it in Ruby. So, to borrow a term from the pro-audio and encoding industry, I transcoded PHP to Ruby (is there a programming equivalent of this word?) using Notepad++ and some regular expressions.

This is far from complete, but it might be a good starting place if you want to convert a PHP project to Ruby:

1. strip < ?php ?> tags at start/end of script

2. replace function with def

3. replace } with end

4. remove {

5. replace // with #

6. replace /*…*/ with #

7. replace null with nil

8. replace $this-> with @

9. replace \$([a-zA-Z0-9]+) with \1

10. replace (\)\w*;) with )

11. remove ;$

12. replace -> with .

13. replace is_string\(([a-zA-Z0-9]+)\) with \1.is_a?String

14. replace is_array\(([a-zA-Z0-9]+)\) with \1.is_a?Array

15. replace else if with elsif

16. replace new\W*([a-zA-Z0-9]+)\W*\( with \1.new(

17. replace .= with +=

18. replace strlen\((@*[a-zA-Z0-9]+)\) with \1.length

19. replace count\((@*[a-zA-Z0-9]+)\) with \1.length

20. replace \W*instanceof\W* with .is_a?

21. replace strtolower\(([a-zA-Z0-9\[\]@\.=’,\(\)]+)\) with \1.downcase


Comments are closed.