Posts tagged “PHP” 
-
Computer
-
PHP
Inspired by this post on Laravel forums.
In short, suppose you're writing a simple oldschool browser quest game where the user makes choices to advance in the story. Or perhaps you're making an online testing resource and want the user to be able to save his progrss without the need to register or keep track of his sessions – by simply bookmarking the current page.
Choices can be saved directly like this:
…However, it's not really great:
«…»
|
Read rest of this entry
»
-
My programs
-
Web
A personal RSS/Atom feed in every home.
Feeder is a syndication framework for PHP 5+. It generates RSS 0.92, RSS 2.0 and Atom feeds from single data source. Bundled Text Feeder generates feeds from text files so you don't have to do any coding at all to get your channel running. And, since it's part of Feeder, it outputs in all three supported feed formats.
Download & license
All Feeder classes and related data is released under public domain. If you use it I'll appreciate a backlink to this page; if you have feedback – feel free to drop a comment.
Last Feeder update was on 9 May 2012 – version 1.1.
«…»
|
Read rest of this entry
»
-
My programs
-
Web
Standalone PHP class for processing bencoding-encoded data, such as bit-torrent files.
BEncoded is a PHP OOP wrapper for two lightenc.php functions
from Theory.org for manipulating bencoded data.
It lets you transform bencoded files, retrieve data from them, add some, delete, calculate
info hashes and do other things using a simple interface.
Can't want to see? Download now or view its source.
First version on 1 July 2011; last update on 23 January 2012.
Preface
«…»
|
Read rest of this entry
»
-
Computer
My reference document based on official RSS/Atom specifications.
This is a reference document I have made for myself in preparation to writing a universal
PHP feed creator class. I have studied both RSS 0.92
& 2.0 specs and RFC 4287
that stands for The Atom Syndication Format (Atom 1.0).
General info
I'll give a brief intro into the RSS versions here because they might be confusing
at first glance (asides from Atom which has them straightforward).
There are 2 branches of the RSS format: RDF-like (0.90, 1.0 and 1.1) and XML-valid
(0.91, 0.92
and 2.0). The first retains more of the
original Netscape specification, RSS standing for RDF Site Summary. The second
is a simplified, improved and XML-compliant format that is most popular among the two
and Atom (not less than 50% of all feeds according to Wikipedia.
«…»
|
Read rest of this entry
»
-
Computer
-
PHP
gzencode() function exists since PHP 4 but gzdecode() - only since PHP 6.
Build-in PHP function gzencode compresses a string with all the necessary
headings the GZIP specification describes (this
is what differs it from gzcompress that omits those headers). However, there's
no counterpart gzdecode function – at least if you can't rely on PHP 6 yet.
Lucky for us, there's a function called readgzfile that does the same thing as
gzdecode except that it reads GZ data from a file and sends the
decompressed string to stdout. We can use this function to imitate gzdecode:
The only difference between the above code and real gzdecode is that
the latter returns false on error while the above returns null. The reason
for this is because I'm always preferring null over false so I don't have to
remember which functions return which «failure value» – look at standard PHP functions
half of which return null and another – false (and some others – empty string
or -1).
P.S.: you'd probably only need gzdecode in case you need reading or writing
the data in standard GZIP format that's produced by a third party library such as
those from ZLib.net. In case of server-side-only processing you can
use gzcompress / gzdecompress that are both available since PHP 4.
|
Show this entry
»
-
Computer
-
Thoughts
Unified formatting for all electronic documents.
Roughly a year ago I've met (actually, re-met after 3 years) Freeman
who among other crazy ideas had the idea of a global text markup language used wordwide,
international and extensible.
Well, this idea isn't new and he certainly didn't invent all of it. Neither did I.But together we've formed something I'm personally proud of – the ObjectWacko markup language.
ObjectWacko is based on a markup
language initially created for WakkaWiki which was later forked into
WackoWiki which has been abandoned nearly 7 years ago
and is now retaken by another team.
Core principles of a human-writable markup are:
- Less to none hacks and tricks. I mean, look at any Wikipedia's articles with more or less complex formatting (or using templates) – everything beyond a simple table with no col/rowspans or embedded language that differs from the one the page's written in will resort to using HTML.
- Human-readable semantics. Let's look at MediaWiki again: it uses 2 apostrophes to denote an italic text and 3 (three) apostrophes to specify a bold text. To make text bold & italic at the same time you need to use 5 (five) apostrophes. How intuitive this is?
DokuWiki is a bit better: it does use things like asterisks for bold (**bold**) and slashes for italic (//italic//) texts but when it comes to strikethru text it uses… <del> tag. The question arises: why not continue the intuitive sequence of tokens and use double dash (--) to denote a deleted text? Perhaps because the processor could confuse it with a typographic em-dash? - Extensibility. Let's take a step away from normal blogging and forum posting and see how markups usually deal with formulae – they, again, use HTML and/or TeX or similar markup. I don't want to say that TeX is a no – it's most welcome in math expressions, I'm not suggesting to invent another wheel. But the way in which markup is extended (and using formulae is an extension because they're not meant for regular users and most text markups are fine without it – look at BB-codes) is wrong. Let's take a look at MediaWiki again: ...the open disk of center <math>(a, b)</math> and radius ''R''....
So math expression is placed inside (HTML-style again but let's bear with it for now) <math> tags. But what if we need some C++ code? Or Delphi? <c>main();</c>, <delphi>program MyApp;</delphi>, <css>rule.class { font-weight: bold; }</css>… What about language named B? <b>bold?!</b>.
One can say that this problem is made up but it hints at the root: traditional markup languages are not extended, «extensions» are part of the markup. In other words, there's no uniform way to call an arbitrary extension. There's no such concept of extensions to begin with. - Recursive, context-dependent text processing. I daresay that most of current markup problems originate from the fact that there's not enough tokens on the keyboard to represent all necessary formatting. For example, if we take double dash (--) to indicate strikethru text then how we'll deal with typographic em-dash? Okay, we can make a regular expression like
PHP/(?<=\s)--(?=\s)/ → "—" and consider -- having both left and right spaces as em-dash, otherwise it's a formatted text block. But what to do with typographic dash "--" is...? Include " in the regexp? But if we use «*» to format bold text (em-dash "**--**" is used to...) – how to differentiate between strikethru token and the dash? Aww, so many questions…
One way is to escape formatting. And it's a good way. Let's say we'll put a tilde (~) before any token and it'll turn into a plain text (not processed). But it's extremely hard to implement as the approach modern formatters traditionally use is doing regexp-based replacements – and that's probably why so few of them actually support token quoting – because pulling this trick on a regular expression-based formatter requires a lot of headache. Even classic WackoWiki formatter supporting tilde still fails on certain token combinations.
The solution exists but is more complex: create a modelling text processor that will work the same as program code compilers do – with recursion and based on context rather than tags/tokens in a document character «stream». This makes it possible to do text replacements in text nodes (like in JavaScript DOM) rather than everywhere – and this means there can be no «false» tokens as text nodes contain only text.
«…»
|
Read rest of this entry
»
-
Computer
-
Thoughts
Let's say we need to display a code snippet on some web page. Program sources usually use highlighted syntax (but that's not the topic of this post) and also display line numbers. We can do the latter in several different ways and I'll outline them below – but in case you want an instant CSS solution you'll find it below here.
PHP
First off, we can simply enumerate lines using PHP like this:
However, this approach has at least one major downside:
«…»
|
Read rest of this entry
»
-
Computer
-
PHP
A workaround to catch E_FATAL, E_PARSE and other core PHP errors.
PHP has a habit of terminating script with no chances to do anything when a
«non-catchable error» occurs. Here non-catchable means pretty much any error: fatal errors,
parse errors, regular errors and all those errors triggered
by PHP core (constants beginning with E_CORE_ prefix such as E_CORE_ERROR).
However, there's a solution – well, not perfect but at
least it allows us catch even fatal errors that PHP might raise: by means of a
shutdown function. I found
it in
the comments to PHP's set_error_handler function, tested it and
it actually worked – I was able to handle even compile errors.
Here's what we do:
Pretty simple. This way your script can continue execution even after a severe PHP
error (like calling an undeclared function or class). However, there's one point:
«…»
|
Read rest of this entry
»
Недавние комментарии
«Stage Once» – a Visual Novel hacking tutorial
Part 1 – the Bruteforce
Today's vectors
Encoding choices as a secure string
Рога и копыта