Search code examples
phpfilesap-gui

How obtain a line from a text file based in the id of the line?


I have a host name which I read from saplogon.ini file. Now, I load this file into an array say $lines.

When I read the host name by $hostname = $lines[10]; I cannot log in but if I hard code value as $line = "host name"; and then assign it to hostname, $hostname = $line, then it works fine.

Any body has any solution to this? To make my program dynamic I need to read this value from saplogon.ini and use explode to get desired name.

Here is snippet of my code:

$pieces = explode('=', $line, 2);
elseif(!isset($ASHOST))   $ASHOST = $pieces[1];
elseif(!isset($SYSNO)) $SYSNO = $pieces[1];

Funny thing is it does not cause any problem for $SYSNO though I get it by same logic.

Here is my saplogon.ini snippet

[Server]
Item1=cesdsbop01.vco.bpcoco
Item2=192.168.1.198
Item3=cesapplpt1
Item4=cesderlpt01.vco.bpcoco
Item5=cesderlpt03.vco.bpcoco
Item5=00
Item6=cesperlpt01.vco.bpcoco
Item7=cesqerlpt01.vco.bpcoco
Item8=cesderlpt02.vco.bpcoco
Item9=cespilpt.vco.bpcoco


[Database]
Item1=00
Item2=01
Item3=00
Item4=00
Item5=00
Item6=00
Item7=00
Item8=00
Item9=01

Does not work, though I was trying to do the same thing, but added FILE_IGNORE_NEW_LINES as well. I tried IP address for hostname, same thing. Sorry I do not know how to format this code below.

$line = "Item5=cesderlpt03.vco.bpcoco";
$CLIENT = "210";
$system = "Item5";
$a["USER"] = "Adarsh";
$handle = "saplogon.ini";
$lines = file($handle, FILE_IGNORE_NEW_LINES);
$line = $lines[0];
echo $lines[0];
$pieces = explode('=', $line, 2);
if(!isset($ASHOST))  { $ASHOST = $pieces[1];}
$line = $lines[1];
$pieces = explode('=', $lines[1], 2);
if(!isset($SYSNO)){ $SYSNO = $pieces[1];}

function sap_login($ASHOST, $SYSNO, $CLIENT, $USER)
{
$l["ASHOST"] = $ASHOST; //"cesderpap03.uhi.amerco";
$l["SYSNR"] = $SYSNO;
$l["CLIENT"] = $CLIENT;
$l["USER"] = $USER;
$l["PASSWD"] = "adarsh123";
if (isset($GWHOST))     $l["GWHOST"] = $GWHOST;              //             
if (isset($GWSERV))             $l["GWSERV"] =  $GWSERV;
$l["MSHOST"] = "";
$l["R3NAME"] = "ECD";
$l["GROUP"] = "";
$l["LANG"] = "EN";
$l["TRACE"] = "";       //if (isset($TRACE))        $TRACE;                   
$l["LCHECK"] = "";      //if (isset($LCHECK))       $LCHECK;                    
$l["CODEPAGE"] = "1160";    //if (isset($CODEPAGE))     $CODEPAGE;


$sap = @saprfc_open ($l);
saprfc_set_code_page ($sap,$l["CODEPAGE"]);
if(! $sap)
{
echo saprfc_error($sap);
}
else {echo "connected";}
}
sap_login(&$ASHOST, &$SYSNO, &$CLIENT, &$a["USER"])
?>

This code let's me login to SAP. That is what I am trying to achieve. If i don't choose to do, $line = $lines[0]; my code works fine because then it is using the hard coded value from the first line $line = "Item5=cesderlpt03.vco.bpcoco";

So based in the name of item I need the line where the Item is placed. As example if I chose Item5 I want retrive the line of my .ini file where the text Item5=cesderlpt03.vco.bpcoco is located.


Solution

  • if you want capture cesderlpt03.vco.bpcoco with the id Item5 this code you can do it:

    <?php
    
    function array_find($needle, $haystack)
    {
       foreach ($haystack as $item)
       {
          if (strpos($item, $needle) !== FALSE)
          {
             return $item;
             break;
          }
       }
    }
    
    $handle = "saplogon.ini";
    $system = "Item5";
    
    $lines = file($handle, FILE_IGNORE_NEW_LINES);
    
    $line = array_find($system, $lines);
    
    echo $line; 
    
    ?>