<?



function MyImgString($x,$y,$str,$position=l,$x2=0,$os=0)

{



/* x2 = vertice direito, $position (l = left -> padrao, c = center, r = right) */



global $s, $dx, $dy, $black, $im, $font, $truetype;



if ($os == 0) $os = $s;





        if ($truetype == 1) {



                if ($position != "l"){

                        $size = imagettfbbox($os,0,$font,$str);

                        $tw=abs($size[4]-$size[0]);





        switch ($position)

        {



        case 'c':



        $x = $x +  intval( ($x2 - $x - $tw - $dx)/2 ) ;



        break;



        case 'r':



        $x = $x +  intval( ($x2 - $x - $tw - (3 * $dx) ) ) ;



        break;

        }



                }



        ImageTTFText($im, $os, 0, $x+$dx,$y+$dy,$black,$font,$str);



        }

        else ImageString($im,$os,$x+$dx,$y+$dy,$str,$black);



}                                                                        







function add_to_bar ($code)

{

	switch ($code)

	{

	case '00':

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

	break;

	

	case '01':

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '02':

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '03':

		add_bt();

		add_wl();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

	break;



        case '04':

                add_bt();

                add_wt();

                add_bt();

                add_wt();

                add_bl();

                add_wl();

                add_bl();

                add_wt();

                add_bt();

                add_wl();

        break;



        case '05':

                add_bt();

                add_wl();

                add_bt();

                add_wt();

                add_bl();

                add_wl();

                add_bl();

                add_wt();

                add_bt();

                add_wt();

        break;



	case '06':

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '07':

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wl();

	break;



	case '08':

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '09':

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '10':

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '11':

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '12':

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '13':

		add_bl();

		add_wl();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '14':

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '15':

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '16':

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '17':

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wl();

	break;



	case '18':

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '19':

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '20':

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '21':

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '22':

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '23':

		add_bt();

		add_wl();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '24':

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '25':

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '26':

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '27':

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wl();

	break;



	case '28':

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '29':

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '30':

		add_bl();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '31':

		add_bl();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '32':

		add_bl();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '33':

		add_bl();

		add_wl();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '34':

		add_bl();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '35':

		add_bl();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '36':

		add_bl();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '37':

		add_bl();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wl();

	break;



	case '38':

		add_bl();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '39':

		add_bl();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '40':

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '41':

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '42':

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '43':

		add_bt();

		add_wl();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '44':

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '45':

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '46':

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '47':

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wl();

	break;



	case '48':

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '49':

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '50':

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '51':

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '52':

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '53':

		add_bl();

		add_wl();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '54':

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '55':

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '56':

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '57':

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wl();

	break;



	case '58':

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '59':

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '60':

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '61':

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '62':

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '63':

		add_bt();

		add_wl();

		add_bl();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '64':

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '65':

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '66':

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '67':

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wl();

	break;



	case '68':

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '69':

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '70':

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '71':

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '72':

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '73':

		add_bt();

		add_wl();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '74':

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bl();

		add_wl();

	break;



	case '75':

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '76':

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bl();

		add_wt();

	break;



	case '77':

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bl();

		add_wl();

	break;



	case '78':

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '79':

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bl();

		add_wt();

	break;



	case '80':

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '81':

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '82':

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '83':

		add_bl();

		add_wl();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '84':

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '85':

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '86':

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '87':

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wl();

	break;



	case '88':

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '89':

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '90':

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '91':

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '92':

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '93':

		add_bt();

		add_wl();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '94':

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

	break;



	case '95':

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '96':

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

	break;



	case '97':

		add_bt();

		add_wt();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wl();

	break;



	case '98':

		add_bt();

		add_wl();

		add_bl();

		add_wt();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '99':

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

		add_bl();

		add_wl();

		add_bt();

		add_wt();

	break;



	case '100':		/* start */

		add_bt();

		add_wt();

		add_bt();

		add_wt();

	break;

	

	case '101':		/* stop */

		add_bl();

		add_wt();

		add_bt();

		add_wl();

	break;

	}

}





function Modulo11($num,$base=9, $r=0) {



/*

        Autor:

                Pablo Costa <pablo@shark.ib.usp.br>



        Função:

		Calculo do Modulo 10 para geracao do digito verificador 

                de boletos bancarios conforme documentos obtidos 

		da Febraban - www.febraban.org.br 



        Entrada:

                $num: string numérica para a qual se deseja calcularo digito verificador;

		$base: valor maximo de multiplicacao [2-$base]

		$r: quando especificado um devolve somente o resto



        Saída:

                Retorna o Digito verificador.



        Linguagem:

                PHP.



        Observações:

                - Script desenvolvido sem nenhum reaproveitamento de código pré existente.

                - Assume-se que a verificação do formato das variáveis de entrada é feita antes da execução deste script.

*/                                        



$fator = 2;



/* Separacao dos numeros */



for ($i = strlen($num); $i > 0; $i--) {



	$numeros[$i] = substr($num,$i-1,1);			// pega cada numero isoladamente

	$parcial[$i] = $numeros[$i] * $fator;		        // Efetua multiplicacao do numero pelo falor  

	$soma    += $parcial[$i];				// Soma dos digitos 

	if ($fator == $base) $fator = 1; 		        // restaura fator de multiplicacao para 2 

	$fator++;				



}



/* Calculo do modulo 11 */

	if ($r == 0) {

        	$soma *= 10;

		$digito = $soma % 11;

		if ($digito == 10) $digito = 0;

        	return $digito;                       

	}

	elseif ($r == 1){

		$resto = $soma % 11;	

		return $resto;

	}

}





function Modulo10($num) {



/*

        Autor:

                Pablo Costa <pablo@shark.ib.usp.br>



        Função:

		Calculo do Modulo 10 para geracao do digito verificador 

                de boletos bancarios conforme documentos obtidos 

		da Febraban - www.febraban.org.br 



        Entrada:

                $num: string numérica para a qual se deseja calcularo digito verificador;





        Saída:

                Retorna o Digito verificador.



        Linguagem:

                PHP.



        Observações:

                - Script desenvolvido sem nenhum reaproveitamento de código pré existente.

                - Assume-se que a verificação do formato das variáveis de entrada é feita antes da execução deste script.

*/                                        



$fator = 2;



/* Separacao dos numeros */



for ($i = strlen($num); $i > 0; $i--) {



	$numeros[$i] = substr($num,$i-1,1);			// pega cada numero isoladamente

	$parcial10[$i] = $numeros[$i] * $fator;			// Efetua multiplicacao do numero pelo (falor 10)

	$numtotal10 = $numtotal10 . $parcial10[$i]; 		// monta sequencia para soma dos digitos no (modulo 10)

	if ($fator == 2) $fator = 1; else $fator = 2;		// intercala fator de multiplicacao (modulo 10)

}



/* Calculo do modulo 10 */



	for ($i = strlen($numtotal10); $i > 0; $i--) {		

	$numeros[$i] = substr($numtotal10,$i-1,1);			

	$soma += $numeros[$i];				

	}



        $resto = $soma % 10;

        $digito = 10 - $resto;

        if ($resto == 0) $digito = 0;                           

	return $digito;                       



}



function dvbradesco($num) {



       $resto = Modulo11($num,7,1);

       $dig = 11 - $resto;



       if ($resto == 1)

         $dig = "P";

       if ( $resto == 0)

          $dig = 0;

       return $dig;

}                             







function MontaLinha ($num) {



global $str;



/*



Posição 	Conteúdo

1 a 3 		Número do banco

4 		Código da Moeda - 9 para Real

5 		Digito verificador do Código de Barras

6  a 19		Valor (12 inteeeiros e 2 decimais)

20 a 44 	Campo Livre definido por cada banco



*/



# 1. Campo - composto pelo código do banco, código da moéda, as cinco primeiras posições

# do campo livre e DV (modulo10) deste campo



$p1 = substr($str,0,4);

$p2 = substr($str,19,5);

$p3 = Modulo10("$p1$p2");

$p4 = "$p1$p2$p3"; 

$p5 = substr($p4,0,5);

$p6 = substr($p4,5);

$campo1 = "$p5.$p6";





# 2. Campo - composto pelas posiçoes 6 a 15 do campo livre 

# e livre e DV (modulo10) deste campo 



$p1 = substr($str,24,10);

$p2 = Modulo10($p1);  

$p3 = "$p1$p2";

$p4 = substr($p3,0,5);

$p5 = substr($p3,5);   

$campo2 = "$p4.$p5";



# 3. Campo composto pelas posicoes 16 a 25 do campo livre

# e livre e DV (modulo10) deste campo



$p1 = substr($str,34,10);

$p2 = Modulo10($p1);  

$p3 = "$p1$p2";

$p4 = substr($p3,0,5);

$p5 = substr($p3,5);   

$campo3 = "$p4.$p5";



# 4. Campo - digito verificador do codigo de barras

$campo4 = substr($str,4,1);





# 5. Campo composto pelo valor nominal pelo valor nominal do documento, sem

# indicacao de zeros a esquerda e sem edicao (sem ponto e virgula). Quando se

# tratar de valor zerado, a representacao deve ser 000 (tres zeros).



$campo5 = substr($str,5,14);





$linha_digitavel = "$campo1 $campo2 $campo3 $campo4 $campo5"; 



return $linha_digitavel;



}





function ShowBankCode($num) {



global $str; 



$p1 = substr($str,0,3); 

$p2 = Modulo11($p1);  



$bankcode = "$p1-$p2";



return $bankcode;

}







##

## Strings2-Functions

##

## (c) 1998 Alex 'SSilk' Aulbach

##

## These Functions are very practical and if I could program

## C a little bit better it will be placed directly in PHP3.

## But I can't... :-}

##

## $Id: strings2.inc,v 1.2 1998/11/22 14:27:43 sas Exp $





##

## This is for the case, that you want to output something

## if $val is false e.g.

##

## p_0or1($faxnumber,"THERE IS NO FAXNUMBER","Faxnumber: %s");

## 

function o_0or1 ($val,$str1,$str2) {

	if (empty($val) || !$val) {

		if (isset($val)) {

			return(sprintf($str1,$val));

		} else {

			return($str1);

		}

	} else {

		return(sprintf($str2,$val));

	}

}

function p_0or1 ($val,$str1,$str2) {

	print o_0or1 ($val,$str1,$str2);

}



## Replaces all blank-chars with &nbsp;

## This function is used, when you are not willing to let the browser

## break your lines an can be used instead of <NOBR>-Tag

## as very compatible replacement

##

## &nbsp; can also be replaced by a true whitespace which has in

## ISO-latin-1 the code 160

##

function o_nonbsp ($val) {

	return(ereg_replace("[[:blank:]\n\r]","&nbsp;",$val));

}

function p_nonbsp ($val) {

	print o_nonbsp($val);

}





function validate_email ($address) {

        return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'. '@'. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.' . '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $address));

}          





/* funcoes para o fator de vencimento */



    function dateToDays($year,$month,$day)

    {



        $century = substr($year,0,2);

        $year = substr($year,2,2);



        if($month > 2)

            $month -= 3;

        else

        {

            $month += 9;

            if($year)

                $year--;

            else

            {

                $year = 99;

                $century --;

            }

        }



        return ( floor((  146097 * $century)    /  4 ) +

                floor(( 1461 * $year)        /  4 ) +

                floor(( 153 * $month +  2) /  5 ) +

                    $day +  1721119);



    }                                         



    function fatorvenc($year,$month,$day)

    {

        return(abs((dateToDays("1997","10","07"))

                    - (dateToDays($year,$month,$day))));

    }                  



?>

