Friday, January 4, 2013

Online Interpreter (and Pastebin) for most popular programming languages


online php interpreter

Google search for that search string reveals this handy webapp:
http://codepad.org/

Which I used, and resulted in this result, which I am able to share with the rest of the world:
http://codepad.org/XSQScOV1

Something like GitHub Gist or pastebin, only better.




So, what wrought this state of affairs?

1
Actually, I needed to write a regular expression condition in PHP, and I also wanted to check that all the special characters required to be escaped, ...are escaped.

To do that manually would be an error-prone affair, so:

php regex which need escape
http://stackoverflow.com/questions/1789382/php-escaping-regex-reserved-characters-anyone-know-whats-wrong-with-this
Answer: Why not simply use preg_quote?

http://php.net/manual/en/function.preg-quote.php
preg_quote() takes str and puts a backslash in front of every character that is part of the regular expression syntax. This is useful if you have a run-time string that you need to match in some text and the string may contain special regex characters.

The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

Last time I didn't know what did preg_match, preg...etc. meant... somehow sounds like a /contraction/ for 'pregnant'.
Okay, but it actually refers to 'Perl-style REGular expression'... thus, 'preg'.




2
Learnt from these:
how to write a regular expression for php

http://www.noupe.com/php/php-regular-expressions.html
http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers
http://www.macronimous.com/resources/writing_regular_expression_with_php.asp
\s Matches any whitespace character including space, tab, form-feed, etc.
\S Matches any non-whitespace character.

As for the third link, it feels ironic that an info page on how to use PHP is written using M$ ASP.
(Btw, so is w3schools.)




3
Using PHP in codepad initially gave me a syntax error though:
http://stackoverflow.com/questions/9135784/syntax-error-unexpected-t-variable

Parse error: syntax error, unexpected T_VARIABLE in xxxx.php on line 2

There is no semicolon at the end of that instruction causing the error.
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement.

Never had to type a semicolon in recent days.


Anyway, the correct code:
<?php
$str = 'abc() !#$%^"\'&*()';
$result = preg_quote ( $str );
echo $result;
?>

The output:
abc\(\) \!#\$%\^"'&\*\(\)

No comments:

Post a Comment