Qualcuno mi può aiutare a convertire questo script da perl a php??
grazie 1000
codice:#!/usr/bin/perl # # Simple program that uses sub EAN13 # It reads standard input for codes and output # the encoded string. # while ( !eof(STDIN) ) { $r = <STDIN>; $X = EAN13($r); if ( $X != -1 ) { print "$X\n"; } else { print " \n"; } } sub EAN13 { # # Returns the string that encodes the first argument of the function # (or -1 if something goes wrong) # in order to be rendered using ean13.ttf , the GPL font from http://grandzebu.net/ # It accepts 12 or 13 chars in input. # If you supply 13 digits, it check if the checksum is right. # # Input: a string containing the code # Output: encoded string or -1 # # Simone Fioravanti, simospa@gieffeedizioni.it # my $inputCode = @_[0]; my $i; my $first; my $checksum = 0; my $barcode = ""; my $inTableA; my $temp; # # Don't ask why, but leave it there. # Took more time than the whole code to figure out!! # $inputCode =~ s/[^0-9]//g; # # Allow 13 digits input - let's trust our users for now # if ( $inputCode =~ /^[0-9]{12}[0-9]?$/ ) { for ( $i = 1 ; $i < 12 ; $i += 2 ) { $checksum += ord( substr( $inputCode, $i, 1 ) ) - 48; } $checksum *= 3; for ( $i = 0 ; $i < 12 ; $i += 2 ) { $checksum += ord( substr( $inputCode, $i, 1 ) ) - 48; } $temp = ( 10 - $checksum % 10 ) % 10; # # Allow 13 digits input only if the 13th is a correct checksum # if ( ( $inputCode =~ /^[0-9]{13}$/ ) && ( substr( $inputCode, 12, 1 ) == $temp ) ) { $inputCode = substr( $inputCode, 0, 12 ); } if ( ( $inputCode =~ /^[0-9]{13}$/ ) && ( substr( $inputCode, 12, 1 ) != $temp ) ) { return (-1); } $inputCode .= $temp; chomp($inputCode); $barcode = substr( $inputCode, 0, 1 ) . chr( 65 - 48 + ord( substr( $inputCode, 1, 1 ) ) ); $first = ord( substr( $inputCode, 0, 1 ) ) - 48; for ( $i = 2 ; $i <= 6 ; $i++ ) { $inTableA = 0; if ( $i == 2 ) { if ( $first >= 0 && $first <= 3 ) { $inTableA = 1; } } elsif ( $i == 3 ) { if ( $first == 0 || $first == 4 || $first == 7 || $first == 8 ) { $inTableA = 1; } } elsif ( $i == 4 ) { if ( $first == 0 || $first == 1 || $first == 4 || $first == 5 || $first == 9 ) { $inTableA = 1; } } elsif ( $i == 5 ) { if ( $first == 0 || $first == 2 || $first == 5 || $first == 6 || $first == 7 ) { $inTableA = 1; } } elsif ( $i == 6 ) { if ( $first == 0 || $first == 3 || $first == 6 || $first == 8 || $first == 9 ) { $inTableA = 1; } } if ($inTableA) { $barcode .= chr( 65 - 48 + ord( substr( $inputCode, $i, 1 ) ) ); } else { $barcode .= chr( 75 - 48 + ord( substr( $inputCode, $i, 1 ) ) ); } } $barcode .= "*"; for ( $i = 7 ; $i <= 12 ; $i++ ) { $barcode .= chr( 97 - 48 + ord( substr( $inputCode, $i, 1 ) ) ); } $barcode .= "+"; } else { $barcode = -1 } return $barcode; }

Rispondi quotando