Utilities
From PHPDevShell
(written on July 15, 2009 for version 2.7.1)
Although PHPDevShell is completely OOP (Object Oriented), sometimes a plain old function can be handy to deal with little but repetitive programming tasks. You can provide one or several files which will be available for all yours scripts, and PHPDevShell itself provides you with a bunch of useful functions.
Contents |
Configuration
Put your file(s) inside the "includes" folder, then list them in the configuration as follow:
$configuration['functions'] = array('utils.php');
From now on, all listed files will be included automatically, therefore any functions (and classes) defined in theses files will be available in every script.
Functions
URL related
These functions are made to make building URL easier, specifically the GET values. The parameters can be seen as an associative array (as the superglobal $_GET), into which you can add/change pairs and from which you can delete elements. The functions then concatenates the content into a string which can be use inside a URL.
function PU_BuildGET ($includeInGet = null, $excludeFromGet = null, $glue = '&')
- Actually concatenates a string based on the current content of
$_GET, which the pairs from$includeInGetincluded, and the list from$excludeFromGetremoved, bound together which the$gluestring. Note that the default glue ('&') is adapted to HTML, if you want to use for example in JavaScript, you should set it to a single'&'. Note also that the resulting string is prefixed with'?'(to be concatenated to an address part of the URL) if needed, which means an empty result would give an empty string (not a single'?').
function PU_BuildURL ($target = null, $includeInGet = null, $excludeFromGet = null, $glue = '&')
- This function builds on the previous one and deal with the address part. The target is supposed to be raw URL of the targeted script (not related to the "target" parameter of HTML links), and defaults to the current script. It's handy even in the simplest form:
PU_BuildURL();
- would give a complete URL pointing to the current script, keeping all current GET parameters along (useful for script handling forms).
function PU_BuildHREF ($label, $includeInGet = null, $excludeFromGet = null, $target = null)
- This function is an extention of the previous one; it does the same job but also encapsulate inside HTML link tag ('<a>'), with
$labelbeing the underlined word.
Examples
Let's say the current script is http://www.phpdevshell.com/url_sample.html?id=123456&action=delete
| code | result |
|---|---|
PU_BuildGET(null, 'action')
| ?id=123456
|
PU_BuildGET(null, array('action', 'id))
| (empty string) |
PU_BuildGET(array('cancel'), 'action')
| ?id=123456&cancel
|
PU_BuildGET(array('cancel' => true), 'action')
| ?id=123456&cancel=true
|
PU_BuildURL(null, null, 'action')
| http://www.phpdevshell.com/url_sample.html?id=123456
|
PU_BuildURL('/check_id.html', null, 'action')
| /check_id.html?id=123456
|
PU_BuildHREF('Check me', '/check_id.html', null, 'action')
| <a href="/check_id.html?id=123456">Check me<a>
|
string related
Building a HTML page is simple as it is a long text; however, not being careful with the string you include in that text can lead to non-functional pages, visual quirks, or even security failures. Theses function will help you secure your HTML.
- function PU_CleanString ($string, $clean_htlm = false)
- This function cleans a string be removing so-called "escape characters" (namely single and double quotes, and backslashes). If asked, it will also removed any XML marking (ie anything between < and >). The resulting string can then be included into a form, for example. Please note the bad parts are actually REMOVED, not converted.
- function PU_MakeString ($string, $htmlize = false)
- This function converts a latin or UTF string into a UTF8 string. Useful when you're not sure which encoding the input string is in. If
$htmlizeis true, the content is also converted to HTML, so it appears correctly in the page.
AJAX related
PHPDevShell tries to make ajax support easier. The same way you can have a single page for form display and handling, you can have a single page HTML and AJAX with this call:
function PU_isAJAX ($json = false)
- This function returns true is the page is requested the AJAX way. If
$jsonis true, is also flushes all current HTML in the buffers, and sets the headers so you then just "print" json data.

