4
Jan

Prüfziffer der ISIN in PHP

categories Computer, PHP     comments Comments (0)

Die ISIN (International Securities Identification Number) ist eine Kennung für Wertpapiere. Wie viele andere solcher Kennungen, beinhaltet auch die ISIN ein Prüfziffer-Verfahren. Wie das in Theorie funktioniert, hat jemand richtig gut bei Wikipedia beschrieben.

Hiermit zeige ich eine kleine Klasse, wie man das in PHP machen kann (use with care and at own risk!). Ursprünglich hatte ich diesen Code in 2010 veröffentlicht, in 2018 arbeitete ich die Hinweise einiger Nutzer ein und aktualisierte die Routinen zu PHP7.

declare(strict_types=1);

/**
 * Validierung einer ISIN (double at double-method)
 * 2010-2018 von Björn A. Dietz
 */
class isin_validate{
	public $isin;
	private $cvok;

	function __construct($isin = false) {
		if ($isin) $this->isin = $isin;
	}

	public function isin_check() {
		$isin = strtoupper($this->isin);
		$s=""; $cv = 0;
		$this->cvok = false;

		for ($i=0;$i<=10; $i++) {
			$b = substr($isin,$i,1);
			if (ord($b)>=65 && ord($b)<=90) $b = ord($b)-55;
			$s .= $b;
		}

		$s = strrev($s);

		for ($i= 0;$i<=strlen($s);$i++) {
			$b = (int) substr($s,$i,1);
			if (!bcmod((string) $i, "2")) $b = $this->quersumme($b * 2);
			$cv += $b;
		}
		
		$cv = 10 - bcmod((string) $cv, "10");
		$cv = bcmod ((string) $cv, "10");

		if ($cv == substr($isin,-1,1)) $this->cvok = true;
		return $this->cvok;
	}

	private function quersumme($zahl) {
		$strZahl = (string) $zahl;
		for( $intQS = $i = 0; $i < strlen ($strZahl); $i++ ) {
			$intQS += (int) $strZahl{$i};
		}
		return (int) $intQS;
	}
}

…und so kann man die Klasse verwenden:

/* Anwendungsbeispiel */
$cv_test = new isin_validate("DE000BAY0017");
echo $cv_test->isin_check();

Ich weiß, das geht immer noch besser… so ist es aber schön übersichtlich!

13
Feb

50 Shades of Grey

categories Aktuelles, Computer, PHP     comments Comments (0)

Well, everybody’s talking about it, don’t they? 🙂

header("Content-type: image/png");
$head = "50 Shades of Grey";
$im = imagecreate(500,280);
$background_color = imagecolorallocate($im, 0, 0, 0);

$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($head)) / 2;
imagestring($im, 3, $px, 9, $head, $orange);

$colc = 5;

for ($i = 0; $i < 500; $i = $i + 50) {
    for ($j = 29; $j < 250; $j = $j + 50) {
        $col = imagecolorallocate($im, $colc, $colc, $colc);
        imagefilledrectangle($im, $i+1, $j+1, $i+49, $j+49, $col);
               
        $coln = str_pad(dechex($colc), 2, "0", STR_PAD_LEFT);
        imagestring($im, 2, $i+2, $j+2, "#".$coln.$coln.$coln, $orange);
        $colc += 5;
    }
} 

imagepng($im);
imagedestroy($im); 

4
Mar

Bash + Ping: Output with Timestamp

categories Computer     comments Comments (0)

One of the many things bash is so great to work with, is the flexibility to combinate the hardened tools at hand to create your desired output.

For example, I wanted to ping a host frequently (lets say every two minutes), and just add a readable timestamp to the output. Usually I’d use a network monitoring tool like Nagios for this task, but in this case it was just to discover when a host comes back alive and pop it onto my screen.

After a few minutes of putting pieces of bash commands together, I solved it this way:

ping -i 120 www.google.com | while read x; do echo "$(date +%T) $x"; done

Result:

08:10:24 PING www.google.com (73.94.35.76) 56(84) bytes of data.
05:10:24 64 bytes from 73.94.35.76: icmp_seq=1 ttl=57 time=9.82 ms
05:12:24 64 bytes from 73.94.35.76: icmp_seq=2 ttl=57 time=9.25 ms
05:14:24 64 bytes from 73.94.35.76: icmp_seq=3 ttl=57 time=24.4 ms
05:16:24 64 bytes from 73.94.35.76: icmp_seq=4 ttl=57 time=9.81 ms
05:18:24 64 bytes from 73.94.35.76: icmp_seq=5 ttl=57 time=60.4 ms

Mission accomplished!

29
Jun

Profiling with MySQL

categories Computer, MySQL, Tutorials     comments Comments (1)

Whenever you encounter a query slower than you expected, and you’re sure you’ve done everything in your power to ensure your tables are properly optimized and indexed, you might want to profile it. MySQL has an inbuild profiler, which allows you to see very detailed for what part of the query how much time has been spend.

To use it, follow these steps:

(1) Activate the profiler.

Since the profiler is deactivated by default we first have to start it.

SET profiling = 1;

(2) Execute your query.

(3) Find out the query id for profiling.

SHOW PROFILES;

It will return you something like this:

    Query_ID |  Duration | Query
    ---------+-----------+-----------------------
      ...    | ...       | ...    
       29    | 0.0006200 | SHOW STATUS
       30    | 0.3600000 | SELECT (your query here)
      ...    | ...       | ...

Now you know the query id is 30.

(4) Profile the query.

SHOW PROFILE FOR QUERY 30;   // example 

This will return you the details, which might look like this:

    Status                          | Duration
    --------------------------------+-------------------
    starting                        | 0.000010
    checking query cache for query  | 0.000078
    Opening tables                  | 0.000051
    System lock                     | 0.000003
    Table lock                      | 0.000008
    init                            | 0.000036
    optimizing                      | 0.000020
    statistics                      | 0.000013
    preparing                       | 0.000015
    Creating tmp table              | 0.000028
    executing                       | 0.000602
    Copying to tmp table            | 0.000176
    Sorting result                  | 0.000043
    Sending data                    | 0.080032
    end                             | 0.000004
    removing tmp table              | 0.000024
    end                             | 0.000006
    query end                       | 0.000003
    freeing items                   | 0.000148
    removing tmp table              | 0.000019
    closing tables                  | 0.000005
    logging slow query              | 0.000003
    cleaning up                     | 0.000004

In this example, most of the time was actually spend sending the data from the server back to the client. Maybe try narrowing down the amount of data you get from the query. Maybe raise your query cache. Maybe do something different – its entirely up to your database, server and needs what to make of this result. But now you know where to look!

4
Apr

Firebug: logging features

categories Computer, Tutorials     comments Comments (0)

Many developers think of Firebug as the mother of all web tools. There’s a reason for it, because Firebug contains sophisticated tools to do nearly everything you need being a web developer, starting with little css experiments, debugging complex jscript-applications, profiling of web pages and many more.

Not commonly known is how to create your own logs using JScript.

For example, …

console.log('Hello World!');

prints exactly this in your Firebug console.

You can refine this, since most of you distinguish informations, warnings, errors and debug output.

console.debug('Debug');
console.info('Information');
console.warn('Warning');
console.error('Error');

Debugging variables can be helped, too.

var banana = 1.234;
var orange = "Test";
console.log("my %a has the value %d", orange, banana);

If you have the need to group log output, just do it like this:

console.group('myGroup');
    console.info('Info1');
    console.debug('What did I want to know?');
console.groupEnd();

Even more features, p.e. timing values, stacks and so on, are documented here.

By the way:
Most of these features have already found their way into the debugger used by Google Chrome!

Next Page »