asp convert to php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

asp convert to php

Post by jackie111 »

Can anyone help me to convert the following ASP code to PHP code:

Code: Select all

Set f = File.OpenTextFile(Server.MapPath(f1.name) , 1)
C=f.readline
Name=Replace(Lcase(f1.name),&quote;.jpg&quote;,&quote;&quote;)
St=Replace((mid(c, Instr(C,&quote;ST:&quote;)+3,Instr(c,&quote;C&quote;)-25)),&quote; &quote;,&quote;&quote;)
CO=Replace((mid(c, Instr(C,&quote;C&quote;)+3,Instr(c,&quote;+X:&quote;)-36)),&quote; &quote;,&quote;&quote;)
X=Cint(Replace((mid(c, Instr(C,&quote;+X:&quote;)+3,Instr(c,&quote;-X:&quote;)-43)),&quote; &quote;,&quote;&quote;))+Cint(Replace((mid(c, Instr(C,&quote;-X:&quote;)+3,Instr(c,&quote;+Y:&quote;)-52)),&quote; &quote;,&quote;&quote;))
Y=Cint(Replace((mid(c, Instr(C,&quote;+Y:&quote;)+3,Instr(c,&quote;-Y:&quote;)-61)),&quote; &quote;,&quote;&quote;))+Cint(Replace((mid(c, Instr(C,&quote;-Y:&quote;)+3,Instr(c,&quote;AX:&quote;)-70)),&quote; &quote;,&quote;&quote;))
TIA
Jackie
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I don't know ASP but if you explain what it does (it looks like a load of seraching and replacing text file contents?) I might be able to churn some code out :)
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

Post by jackie111 »

Thanks for your help. I need to get some information from a special file, i can open the file via notepad, and the top lines are like:
LA:pp09
ST: 27097
CO: 0
+X: 2594
-X: 431
+Y: 96
-Y: 2558
AX:+ 1027
AY:- 2232

I need to get the following info from the file:
LA, ST, CO, X=+X-(-X) Y=(+Y)-(-Y)

I am not familar with php, You could use php to open a text file, then get the value from there, For the correct value of position, please check my ASP code.
thanks
Jackie


d11wtq wrote:I don't know ASP but if you explain what it does (it looks like a load of seraching and replacing text file contents?) I might be able to churn some code out :)
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Code: Select all

$handle = fopen("inputfile.txt", "r");
while (!feof($handle)) {
    $str = fgets($handle, 4096);
    $aray = explode(":",$str);
    $neededStr = $aray[0];
    echo $neededStr.'<br>';
}
fclose($handle);
But this part...
jackie111 wrote: X=+X-(-X) Y=(+Y)-(-Y)
.. i don't understand. Is it a math function?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Wasn't sure about the explode() idea since I dunno if the colon may occur anywhere else but in theory it would be more efficient if benchmarked...

The maths works too.. makes nastly looking regex though since there seems to be random whitespace between the pos/neg signs....

Code: Select all

$file = '/full/path/to/file.txt';
$string = file_get_contents($file);

$results = array();

//LA
preg_match('#^LA\:(?: )*(.*)#m', $string, $match);
$results['LA'] = trim($match[1]);
//ST
preg_match('#^ST\:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
$results['ST'] = trim($match[1].$match[2]);
//CO
preg_match('#^LA\:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
$results['CO'] = trim($match[1].$match[2]);
//X
preg_match('#^\+X:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
$X0 = trim($match[1].$match[2]);
preg_match('#\-X:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
$X1 = trim($match[1].$match[2]);
$results['X'] = $X0 - $X1;
//Y
preg_match('#^\+Y:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
$Y0 = trim($match[1].$match[2]);
preg_match('#\-Y:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
$Y1 = trim($match[1].$match[2]);
$results['Y'] = $Y0 - $Y1;

//Just to show the output <-- delete this
print_r($results);
If you need a easy to use function:

Code: Select all

function getData($file) {
    $string = file_get_contents($file);

    $results = array();

    //LA
    preg_match('#^LA\:(?: )*(.*)#m', $string, $match);
    $results['LA'] = trim($match[1]);
    //ST
    preg_match('#^ST\:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
    $results['ST'] = trim($match[1].$match[2]);
    //CO
    preg_match('#^LA\:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
    $results['CO'] = trim($match[1].$match[2]);
    //X
    preg_match('#^\+X:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
    $X0 = trim($match[1].$match[2]);
    preg_match('#\-X:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
    $X1 = trim($match[1].$match[2]);
    $results['X'] = $X0 - $X1;
    //Y
    preg_match('#^\+Y:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
    $Y0 = trim($match[1].$match[2]);
    preg_match('#\-Y:(?: )*([\-\+])?(?: )*(.*)#m', $string, $match);
    $Y1 = trim($match[1].$match[2]);
    $results['Y'] = $Y0 - $Y1;

    return $results;
}

//Example
$data = getData('full/path/to/file.txt');
print_r($data); //Test only
Essentially... it opens the file and reads ALL the data. Runs some regex to get what it needs then does the calulation on the bits you need. It adds everything to an array so you can call it back easily.

Hope it helps.
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

Post by jackie111 »

thanks so much it do not works well. here is your code:

Code: Select all

function getData($file) {    
$string = file_get_contents($file);     
$results = array();     
//LA    preg_match('#^LA\:(?: )*(.*)#m', $string, $match);   
 $results&#1111;'LA'] = trim($match&#1111;1]);    
 //ST    preg_match('#^ST\:(?: )*(&#1111;\-\+])?(?: )*(.*)#m', $string, $match);    
 $results&#1111;'ST'] = trim($match&#1111;1].$match&#1111;2]);    
 //CO    preg_match('#^LA\:(?: )*(&#1111;\-\+])?(?: )*(.*)#m', $string, $match);    
 $results&#1111;'CO'] = trim($match&#1111;1].$match&#1111;2]);   
  //X    preg_match('#^\+X:(?: )*(&#1111;\-\+])?(?: )*(.*)#m', $string, $match);   
  $X0 = trim($match&#1111;1].$match&#1111;2]);    
  preg_match('#\-X:(?: )*(&#1111;\-\+])?(?: )*(.*)#i', $string, $match);    
  $X1 = trim($match&#1111;1].$match&#1111;2]);    
  $results&#1111;'X'] = $X0 - $X1;    
  //Y    preg_match('#^\+Y:(?: )*(&#1111;\-\+])?(?: )*(.*)#m', $string, $match);   
   $Y0 = trim($match&#1111;1].$match&#1111;2]);   
    preg_match('#\-Y:(?: )*(&#1111;\-\+])?(?: )*(.*)#i', $string, $match);    
    $Y1 = trim($match&#1111;1].$match&#1111;2]);   
     $results&#1111;'Y'] = $Y0 - $Y1;    
      return $results;} 
      //Example
      $data = getData('2/freebie.dst');
      print_r($data); //Test only
error below:
Notice: Undefined variable: match in E:\design\test.php on line 6

Notice: Undefined variable: match in E:\design\test.php on line 8

Notice: Undefined variable: match in E:\design\test.php on line 8

Notice: Undefined variable: match in E:\design\test.php on line 10

Notice: Undefined variable: match in E:\design\test.php on line 10

Notice: Undefined variable: match in E:\design\test.php on line 12

Notice: Undefined variable: match in E:\design\test.php on line 12
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You haven't copied the code correctly...

Sorry, it's easier if you use Firefox to copy & paste since the

Code: Select all

tags are buggy.

Another way is to click "Quote" on my post and copy the code form there... it does work
jackie111
Forum Newbie
Posts: 13
Joined: Thu Jun 23, 2005 3:16 am

Post by jackie111 »

I am use the correct code, but had made a mistake while copying it to here.
not the correct code, i will find another way to do that.
thanks anyway
jackie
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

jackie111 wrote:I am use the correct code, but had made a mistake while copying it to here.
not the correct code, i will find another way to do that.
thanks anyway
jackie
I have tested it... I get the identical errors you have there when I use *your* code.
But when using *my* code I get a clean output.

All the preg_...()'s are commented out in yours - hence the errors regarding undefined variable since no $match array is being created - the errors fit perfectly with what you posted.

I can't give you another solution since the code I posted does what you asked for sorry. Seriously, copy the code again or break it down by using the PHP manual and see how it works rather than just attempting to copy and paste incorrectly :?
Post Reply