Skip to content

Instantly share code, notes, and snippets.

@apphp-snippets
apphp-snippets / gist:4013518
Created November 4, 2012 20:19
One of the important things about user input is to verify that the user has supplied the email that you have requested. The function below allows you to verify email address with easy.
<script type="text/javascript">
/* Source: http://www.apphp.com/index.php?snippet=javascript-email-validation */
function check_email(email){
var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(reg.test(email)){
return true;
}else{
return false;
}
}
@apphp-snippets
apphp-snippets / gist:4013504
Created November 4, 2012 20:18
The function that closes window is very useful when you use a pop-up window on your page, because it allows the visitor to easily close the window. You can also do it several ways: to use a button, a text link or make the window closes automatically after
<!-- Sample #1: form button -->
/* Source: http://www.apphp.com/index.php?snippet=javascript-close-window */
<form>
<input type=button value="Close This Window" onclick="javascript:window.close();">
</form>
<!-- Sample #2: text link -->
<a href="javascript:window.close();">Click Here to Close Window</a>
<!-- Sample #3: close window after a given number of seconds -->
@apphp-snippets
apphp-snippets / gist:4013493
Created November 4, 2012 20:17
You may perform this work with two ways: pass it directly like described in sample #1 or use an anonymous function like its shown in sample #2.
<script type="text/javascript">
/* Source: http://www.apphp.com/index.php?snippet=javascript-pass-parameters-in-setinterval */
// sample #1
setInterval('alert_function(1)', 1500);
// sample #2
setInterval(function(){ alert_function(10); }, 1500);
// user's alert function
function alert_function(val){
@apphp-snippets
apphp-snippets / gist:4013487
Created November 4, 2012 20:16
This function allows you to pass two variables that represent ID of two elements on your page. What you click on one of them it hide it and shows another one.
<script type="text/javascript">
/* Source: http://www.apphp.com/index.php?snippet=javascript-toggle-elements */
function toggle(id, id2){
var toggle_one = document.getElementById(id);
var toggle_two = document.getElementById(id2);
if(toggle_one.style.display == "block"){
toggle_one.style.display = "none";
toggle_two.innerHTML = "Show";
}else{
toggle_one.style.display = "block";
@apphp-snippets
apphp-snippets / gist:4013482
Created November 4, 2012 20:15
This code allows to remove all duplicate elements from an array using PHP array_unique() function.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-remove-duplicates-in-array */
$input = array("a"=>"apple", "pear", "b"=>"apple", "orange", "avocado", "banana");
print_r($input);
$result = array_unique($input);
print_r($result);
?>
@apphp-snippets
apphp-snippets / gist:4013478
Created November 4, 2012 20:14
This code shows how to parse XML file in easy way using PHP.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-parsing-xml-file */
// this is a sample xml string
$xml_string="<?xml version='1.0'?>
<text>
<article id='Article1'>
<title>Title 1</title>
<content>..text here..</content>
</article>
<article id='Article2'>
@apphp-snippets
apphp-snippets / gist:4013475
Created November 4, 2012 20:13
This snippet allows you set a limitation for download rate of the file that visitors download from your site.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-download-file-with-speed-limit */
/* set here a limit of downloading rate (e.g. 10.20 Kb/s) */
$download_rate = 10.20;
$download_file = 'download-file.zip';
$target_file = 'target-file.zip';
if(file_exists($download_file)){
/* headers */
@apphp-snippets
apphp-snippets / gist:4013433
Created November 4, 2012 20:07
This code allows to list the contents of any given directory.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-list-directory-contents */
function list_directory_content($dir){
if(is_dir($dir)){
if($handle = opendir($dir)){
while(($file = readdir($handle)) !== false){
if($file != '.' && $file != '..' && $file != '.htaccess'){
echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."\n";
}
}
@apphp-snippets
apphp-snippets / gist:4013426
Created November 4, 2012 20:06
This code allows to pass filename in the $file_name variable and function will return file extension only.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-file-extension */
function get_file_extension($file_name)
{
/* may contain multiple dots */
$string_parts = explode('.', $file_name);
$extension = $string_parts[count($string_parts) - 1];
$extension = strtolower($extension);
return $extension;
}
@apphp-snippets
apphp-snippets / gist:2770294
Created May 22, 2012 17:03
Sometimes it's desirable to change a style of the first and/or last elements in a container. You can do this by manually applying classes to your HTML elements: ("last-child" still not supported in IE8).
<style type="text/css">
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
p.first { margin-top: 0 !important; margin-left: 0 !important; }
p.last { margin-bottom: 0 !important; margin-right: 0 !important; }
/* or */
div#articles p:first-child { border:1px solid #c1c13a; }
div#articles p:last-child { border:1px solid #3ac13a; }
/* or */
div#articles > :first-child { text-align:left; }