<?php
  //$linha = "if( \$nComp != '' ) \$texto .= '  ); '.\$nl;";
  
//  $linha = "if ( substr(\$value, 1, 1) == '\"' && substr(\$value, -1, 1) == '\"')";
//  $linha .= "{ \$value = substr(\$value, 1, -1); }";

  $linha = 'fwrite( handle, "" . $key . " = " . $value . \" )';

  echo chr(27)."\<pre>";
  echo $linha.'<br><br>';
  echo"abre = ".contaCaracter($linha, ')')."<br>";
  echo"fecha = ".contaCaracter($linha, '(')."<br>";

function contaCaracter( $linha, $char )
{
  $ret    = 0;
  $flag_C = true;
  $flag_B = true;

  $len = strlen( $linha );

  for( $ix = 0; $ix < $len; $ix++ )
  {
    if( $linha[$ix] == '\\' && $linha[$ix+1] == '"')
      $ix = $ix +2;
    if ($linha[$ix] == '"' && $flag_B)
      $flag_C = !$flag_C;
    if ($linha[$ix] == "'" && $flag_C)
      $flag_B = !$flag_B;

echo $linha[$ix];

    if( $flag_C &&
        $flag_B &&
        $linha[$ix] == $char
       )
    {
      $ret ++;
    }// End if()
  }// End for()
  return $ret;
}// End function()

function conta_Caracter( $linha, $char )
{
  $ret = 0;
  $len = strlen( $linha );
  $ix_ = strpos( $linha, $char );

  if( $ix_ === false )
  {
    $ret = 0;
  }else
  {
    $ixx = $ix_;
    while( !($ixx === false) )
    {
      $ixx = strpos( $linha, $char, $ixx +1 );
      $ret ++;
    }// End if()
  }
  return $ret;
}// End function()

  $linha = 'include( retRegistro(), fechar() );';
  echo"abre = ".contaCaracter($linha, ')')."<br>";
  echo"fecha = ".contaCaracter($linha, '(')."<br>";
  
?>
