Page 1 of 1
Reading text-string
Posted: Thu Aug 27, 2009 8:12 am
by jorgeng
Hello!
I have a text-string looks like this:
OM Sweden:SE0007286893:SEK;2;2;0;8 END
I want to read the 8 at right before end to a constant.
My problem is that sometimes it is 8, sometimes 16, and sometimes 140
and then the textstring looks like this:
OM Sweden:SE0007286893:SEK;2;2;0;16 END
OM Sweden:SE0007286893:SEK;2;2;0;140 END
meaning END doens't always occurs in same position, how do i solve this?
I have used this code before, but it read from left to right:
Code: Select all
// make sure the file is successfully opened before doing anything else
if ($fp = fopen($myFile, 'r')) {
$content = '';
// keep reading until there's nothing left
while ($line = fread($fp, 1024)) {
$content .= $line;
$data = strstr($content,"SE");
$isin = substr($data, 0, 12);
echo "$isin <br />";
}
// do something with the content here
} else {
// an error occured when trying to open the specified file
// do something here ... ?
echo "Kunde ej öppna html-filen";
}
How do i read from right to left to get hit on data before END?

Re: Reading text-string
Posted: Thu Aug 27, 2009 8:17 am
by jackpf
Could do it like this:
Code: Select all
$str = reset(explode(' ', end(explode(';', $str))));
Should work.
Re: Reading text-string
Posted: Fri Aug 28, 2009 3:05 am
by jorgeng
Sorry, doesn't work, i got:
Parse error: parse error in C:\Program Files\xampp\htdocs\R.php on line 111
Code: Select all
$str = "OM Sweden:SE0007286893:SEK;2;2;0;8 END"
$str = reset(explode(' ', end(explode(';', $str))));
echo "$str <br />";
What's wrong?

Re: Reading text-string
Posted: Fri Aug 28, 2009 4:24 am
by susrisha
Code: Select all
$str = "OM Sweden:SE0007286893:SEK;2;2;0;8 END";//you missed the semi colon here
$str = reset(explode(' ', end(explode(';', $str))));
echo "$str <br />";
Re: Reading text-string
Posted: Fri Aug 28, 2009 4:33 am
by jorgeng
susrisha wrote:Code: Select all
$str = "OM Sweden:SE0007286893:SEK;2;2;0;8 END";//you missed the semi colon here
$str = reset(explode(' ', end(explode(';', $str))));
echo "$str <br />";
Thanks, but the string answer i got on ie8 screen display is:
OM Sweden:SE0007286893:SEK;2;2;0;8 END
without ; and I am not sure how explode handle this...
if i put a ; after string it works, but it is not the answer i got from string..
The string finishes at the position after END.

Re: Reading text-string
Posted: Fri Aug 28, 2009 4:49 am
by susrisha
Code: Select all
$str ="OM Sweden:SE0007286893:SEK;2;2;0;140 END";
$str_exploded = explode(';',$str);
$last_elemet = end($str_exploded);
$number = substr($last_elemet,0,(strlen($last_elemet)-4));
echo $number;
This is not a very good code though.. but the output is proper..Have tried it with other strings also.. works fine for me..
Re: Reading text-string
Posted: Fri Aug 28, 2009 5:00 am
by jorgeng
susrisha wrote:Code: Select all
$str ="OM Sweden:SE0007286893:SEK;2;2;0;140 END";
$str_exploded = explode(';',$str);
$last_elemet = end($str_exploded);
$number = substr($last_elemet,0,(strlen($last_elemet)-4));
echo $number;
This is not a very good code though.. but the output is proper..Have tried it with other strings also.. works fine for me..
Thanks, it works good.
My problem is that i can't handle the answer from fp5, can you help me?
I got error:
Parse error: parse error in C:\Program Files\xampp\htdocs\R.php on line 110
Code: Select all
$fp5 = fsockopen("tcp://xx.xx.xx.xx", xxxx, $errno, $errstr);
if (!$fp5) {
echo "$errstr ($errno)<br />\n";
} else {
fputs($fp5, "i;W\n");
while (!feof($fp5))
echo fgets($fp5, 4096);
fclose($fp5);
}
// $str ="OM Sweden:SE0007286893:SEK;2;2;0;140 END";
$str = $fp5
$str_exploded = explode(';',$str);
$last_elemet = end($str_exploded);
$number = substr($last_elemet,0,(strlen($last_elemet)-4));
echo "Unfilled volume:";
echo $number;
Re: Reading text-string
Posted: Fri Aug 28, 2009 5:09 am
by susrisha
parse error...
the same as i mentioned earlier.. you are missing a semicolon.
and i am not sure if you will get the same data after you closed the socket connection..
Code: Select all
$your_string=fgets($fp5, 4096);
//your fclose statements
$str = $your_string;
Re: Reading text-string
Posted: Fri Aug 28, 2009 5:12 am
by susrisha
i took the liberty of taking your code and totally modifying it to your requirement.. this might probably help.
Code: Select all
$fp5 = fsockopen("tcp://xx.xx.xx.xx", xxxx, $errno, $errstr);
if (!$fp5) {
echo "$errstr ($errno)<br />\n";
} else {
fputs($fp5, "i;W\n");
while (!feof($fp5))
{
//echo fgets($fp5, 4096);
$str = fgets($fp5, 4096);
echo $str;
$str_exploded = explode(';',$str);
$last_elemet = end($str_exploded);
$number = substr($last_elemet,0,(strlen($last_elemet)-4));
echo "Unfilled volume:";
echo $number;
}
fclose($fp5);
}
Re: Reading text-string
Posted: Fri Aug 28, 2009 5:16 am
by jorgeng
I tried to not close fp5, but still got error:
Resource id #8
and the string is
OM Sweden:SE0007286893:SEK;2;2;0;8 END

Re: Reading text-string
Posted: Fri Aug 28, 2009 5:19 am
by susrisha
you are trying to assign the file pointer (which is a resource and not a string).
Look at the code i have posted for you and see if that works..
Re: Reading text-string
Posted: Fri Aug 28, 2009 11:20 am
by jorgeng
susrisha wrote:you are trying to assign the file pointer (which is a resource and not a string).
Look at the code i have posted for you and see if that works..
Sorry, your modified code didn't work, BUT i think it can be solved.
If we can inspect (cobol) the fp5 and read characters after # we have a solution, but
i am not able to code that, do you know how to php-code that?

Re: Reading text-string
Posted: Mon Aug 31, 2009 5:41 am
by jorgeng
I tried this to read the 8 but got error:
Warning: fread(): 8 is not a valid stream resource in C:\Program Files\xampp\htdocs\R.php on line 123
Code: Select all
// Strängen som kommer ut ser ut så här: Resource id #8Unfilled volume:Resource i, vi läser ut 8:an
if ($fp6 = fopen($file, 'r')) {
$content = '';
$fp6 = $fp5;
// keep reading until there's nothing left
while ($line3 = fread($fp6, 1024)) {
$content3 .= $line6;
$data3 = strstr($content3,"#");
$unfilled_volume = substr($data3, 5, 6);
echo "$unfilled_volume <br />";
}
// do something with the content here
} else {
// an error occured when trying to open the specified file
// do something here ... ?
echo "Kunde ej läsa unfilled volume";
Do uyou have any other way to read the 8?

Re: Reading text-string
Posted: Mon Aug 31, 2009 10:53 am
by Paul_F
Try this:
Code: Select all
<?php
echo 'Test 1:' . '<br />';
$originalstring = 'OM Sweden:SE0007286893:SEK;2;2;0;8 END';
$mystring = ';' . $originalstring;
$last = substr(strrchr($mystring, ';'), 0 );
echo $last . '<br /><br />';
echo 'Test 2:' . '<br />';
$originalstring = 'OM Sweden:SE0007286893:SEK;2;2;0;140 END';
$mystring = ';' . $originalstring;
$last = substr(strrchr($mystring, ';'), 0 );
echo $last . '<br />';
?>
Results:
Test 1:
;8 END
Test 2:
;140 END