Get current script directory name

To get full path to the script currently being read:

dirname(__FILE__)

To get just the current directory name:

basename(dirname(__FILE__))
Posted in php | Leave a comment

Safe Redirect

Safe redirect:

<?php

function Redirect($Str_Location, $Bln_Replace = 1, $Int_HRC = NULL)
{
        if(!headers_sent())
        {
            header('location: ' . urldecode($Str_Location), $Bln_Replace, $Int_HRC);
            exit;
        }

    exit('<meta http-equiv="refresh" content="0; url=' . urldecode($Str_Location) . '"/>'); # | exit('<script>document.location.href=' . urldecode($Str_Location) . ';</script>');
    return;
}

?>
Posted in php | Tagged | Leave a comment

list() and each() functions usage

$getstrings = explode('&quot;', $splitter.$s.$splitter);

        $delimlen = strlen($splitter);
        $instring = 0;

        while (list($arg, $val) = each($getstrings))
        {

One more example

$data = 'blalll==115||blah2==3||blah4==234';
$d_price_a = explode('||', $data);
list(,$d_price) = explode('==', $d_price_a[0]);
if($d_price)
{
    ...
}

Before using each() method the array must be reset by reset();

Posted in php | Tagged , | Leave a comment

SimpleXmlElement – save indented tree

As everyone should be aware, SimpleXmlElement class is not capable to preserve the indents in xml output.
To achieve that the DOMDocument class comes in handy.

//Format XML to save indented tree rather than one line and save
$dom = new DOMDocument('1.0');
$dom-&gt;preserveWhiteSpace = false;
$dom-&gt;formatOutput = true;
$dom-&gt;loadXML($xml-&gt;asXML());
$dom-&gt;save('fileName.xml');
Posted in XML | Tagged | Leave a comment

Regex quick reference

Regex quick reference

[abc] A single character: a, b or c
[^abc] Any single character but a, b, or c
[a-z] Any single character in the range a-z
[a-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\A Start of string
\z End of string
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit
\D Any non-digit
\w Any word character (letter, number, underscore)
\W Any non-word character
\b Any word boundary character
(…) Capture everything enclosed
(a|b) a or b
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a

options: i case insensitive m make dot match newlines x ignore whitespace in regex o perform #{…} substitutions only once

Posted in php | Tagged | Leave a comment

List All Files in a Directory

Here is the simple piece of code to list all files in a given directory

//path to directory to scan
$directory = "../images/team/harry/";

//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");

//print each file name
foreach($images as $image)
{
echo $image;
}
Posted in php | Tagged | Leave a comment

How to set div to center of the screen

<div id='div_id'>
Blah! Blah!
</div>

To set the div above to the center we can use next JQuery code.

jQuery.fn.center = function () {
    var w = $(window);
    this.css("position","fixed");
    this.css("top",w.height()/2-this.height()/2 + "px");
    this.css("left",w.width()/2-this.width()/2  + "px");
    return this;
}
$("#div_id").center();
Posted in jquery, jquery snippets | Tagged | Leave a comment

Working with select

We have the html code below:

&lt;select name=&quot;sel&quot; id=&quot;sel&quot;&gt;
&lt;option value=&quot;1&quot;&gt;1&lt;/option&gt;
&lt;option value=&quot;2&quot;&gt;2&lt;/option&gt;
&lt;option value=&quot;3&quot;&gt;3&lt;/option&gt;
&lt;option value=&quot;4&quot;&gt;4&lt;/option&gt;
&lt;/select&gt;

Code below set the current value
document.getElementById(&quot;sel&quot;).value = 3

or

function setSelectedIndex(s, v) {
    for ( var i = 0; i &lt; s.options.length; i++ ) {
        if ( s.options[i].value == v ) {
            s.options[i].selected = true;
            return;
        }
    }
}

To extract the selected value the code below will help

Coming soon!

?
Posted in javascript | Leave a comment

How to check and uncheck radio button on click

$('input[type=&quot;radio&quot;]').change(function() {
    $(this).prop('checked', function(i, checked){
       return !checked;
    });
});
Posted in jquery, jquery snippets | Tagged | Leave a comment

Upload file and change permisions in right way

		if(! move_uploaded_file($file['tmp_name'], $this->full_name ) )
			throw new Exception("Error to save the file {$this->full_name}");

		$oldumask = umask(0);

		if( ! chmod( $this->full_name, 0755 ) )
			throw new Exception("Error to set permissions for the file {$this->full_name}");	

		umask($oldumask);
Posted in php | Tagged | Leave a comment

How to select option when page is loaded

<html>
<head>
<script type="text/javascript">
function load()
{
document.getElementById('select_id').value =  24;
}
</script>
</head>

<body onload="load()">
<h1>Hello World!</h1>
</body>
</html>
Posted in javascript, javascript snippets | Tagged | Leave a comment

Include JQuery UI framework from Google libs

Code snippet to include JQuery UI framework from Google libs

&lt;head&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;/&gt;
&lt;/head&gt;
Posted in javascript, javascript snippets, jquery, jquery snippets | Tagged | Leave a comment