Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Benoit Asselin http://www.ab-d.fr

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

parseInt() in PHP

function parseInt($string) {
//	return intval($string);
	if(preg_match('/(\d+)/', $string, $array)) {
		return $array[1];
	} else {
		return 0;
	}
}


echo parseInt("2008"); // 2008
echo parseInt("99.90 dollars"); // 99
echo parseInt("www.w3.org"); // 3
echo parseInt("300 spartiates"); // 300
echo parseInt("block text..."); // 0


Source: AB-D Création de sites internet

Make a variable CSS

Page.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	
	<title>Variable CSS</title>
	<link rel="stylesheet" type="text/css" href="style.php" />
</head>

<body>


<h1>Title</h1>

<p class="color-1"> Text  Text  Text  </p>

<p class="color-2"> Text  Text  Text  </p>

<p class="color-3"> Text  Text  Text  </p>


</body>

</html>


Style.php
<?php
header('Content-Type: text/css');

$color_0 = '#000000';
$color_1 = '#ff0000';
$color_2 = '#ff3300';
$color_3 = '#ff6600';

?>

* { font-family: sans-serif; }

h1 {
	padding: 5px;
	color: <?= $color_0 ?>;
	border: 5px solid <?= $color_2 ?>;
	background-color: <?= $color_3 ?>;
}

p.color-1 { color: <?= $color_1 ?>; }
p.color-2 { color: <?= $color_2 ?>; font-weight: bold; }
p.color-3 { color: <?= $color_3 ?>; font-style: italic; }



Source: Asselin Benoit Développement, création de site internet amiens

count() in JavaScript

The same function as count() in php.

Array.prototype.count = function() {
	return this.length;
};


Sample :
var v_array = [ 5, 10, 15, 20, 25];
document.writeln(v_array.count());  // 5


Source: AB-D.fr

Some problems with charset in UTF-8 ?

So you can use this request MySQL before all others, for fix your problems :
...
mysql_query( "SET NAMES 'utf8' " );
...


Source: ab-d.fr
Languages: PHP and MySQL

Use get_magic_quotes_gpc();

function f_magic_quotes($text) {
	if ( !get_magic_quotes_gpc() ) {
		return addslashes($text);
	} else {
		return $text;
	}
}


function f_clean_quotes($text) {
	if ( !get_magic_quotes_gpc() ) {
		return $text;
	} else {
		return stripslashes($text);
	}
}


Source: ab-d

Write a variable PHP with spaces or accents

Write a variable PHP with spaces or accents

<?php

${'New variable text'} = 'This is a new variable text.';

echo ${'New variable text'};
// This is a new variable text.

?>


Source: ab-d

in_array() in JavaScript

The same function as in_array() in php.

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}


Sample :
var v_array = [ 5, 10, 15, 20, 25];
document.writeln(v_array.in_array(10));  // true
document.writeln(v_array.in_array(11));  // false


Source: PHP Manual in_array()
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS