Jump to content

[SOLVED] PHP - Trimming Data String


pacchiee

Recommended Posts

Hello,

 

I am communicating with a serial device through PHP to receive data (electronic weighing balance). The maximum data received at once is 4600 bytes. Now, I have problems to trim this data string to what actually is necessary.

 

Example, I place some weight and read the data through PHP. Received data string is:

 

+ 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.98

 

Added some more weight and received:

 

+ 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt

 

Removed some weight to receive:

 

+ 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 00

 

With this I could make out that every time some change is made to the weight, the latest weight is appended to the data string. I wanted to read the data by trimming it to one decimal point, something like this:

 

0.9

 

1.2

 

0.0

 

I don't know how exactly to trim the data string received. Please help.

 

Thanks in Advance.

Link to comment
Share on other sites

Hey,

 

Do you want to trim it in the receiving php file?

Is the data s ingle string?

Do you explode the string into an array?

 

Can you show the php script being used?

 

Yes, I want to trim the string in the receiving php file.

 

Yes, the data is a single string. The three examples above are the outputs I got when refreshing the page.

 

Here is the PHP script:

 

<?php
/*if(!extension_loaded('phpDll')) {
dl('phpDll.dll');
}*/
$module = 'win_serial_port';

if (extension_loaded($module)) {
  // $str = "module loaded";
} else {
// $str = "Module $module is not compiled into PHP";
die("Module $module is not compiled into PHP");
}
// echo "$str<br>";

$functions = get_extension_funcs($module);
echo "Functions available in the $module extension:<br>\n";
foreach($functions as $func) {
    echo $func."<br>";
}

echo "<p>";
$str = ser_version();
echo "Version: $str";

echo "<p>";
echo "Open port";
ser_open("COM1", 2400, 8, "None", 1, "None");

echo "<p>";
$str = ser_isopen();
echo "Port: $str";

echo "<p><pre>";
$str = ser_read(512);
echo $str;

?>

 

I am using some dll to read from serial port. The last $str (ser_read(512)) is actually the data string am talking about, which needs to be trimmed to get the appended value in one decimal point.

 

Hope I have made it clear.  :)

Link to comment
Share on other sites

What do you do with the string afterwards.

 

Once way would be to explode the string into an array like this;

 

0001.257 Lt

0001.257 Lt

0001.285 Lt

 

Then strip out the Lt, elaving you with an array of numbers. Then numbers can then be changed to one dp. What do you need after that? Does it need to be rebuit with 'Lt' and the pluses?

Link to comment
Share on other sites

$string = '+ 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 00';

preg_match_all('#(\d+\.\d+)#', $string, $matches);

$numbers = array_map(create_function('$a', 'return round($a, 1);'), $matches[1]);

print_r($numbers);

Link to comment
Share on other sites

preg_match_all, array_map, and create_function?  Wow!  That's a lot of work.

 

$data=explode(" Lt + ",$input_string);
$c=count($data);
for($i=0;$i<$c;++$i) $data[$i]=(float)$data[$i];

 

Unless I have my typecasting wrong, that should do the trick.

Link to comment
Share on other sites

I do not want an array of the numbers from the string actually. For example, let this be the data string:

 

$string = "$string = '+ 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 0000.089 Lt + 000";

 

I want to trim this string in such a way that it displays only the last value in a single decimal point. To make it simple, I need to trim the above string to just "0.0". No need of rounding it to the nearest number or something.

 

Another example:

 

$string = "+ 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0000.988 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.257 Lt + 0001.";

 

I want to trim the above string to just 1.2 (0001.257 is the actual valid last value in the above string)

 

after I get that value, it will be multiplied by some integer and display the result, which would be simple multiplication and rounding off. But am not able to trim the string in the above mentioned fashion.

 

Please help.

Link to comment
Share on other sites

Hello,

 

I am communicating with a serial device through PHP to receive data (electronic weighing balance). The maximum data received at once is 4600 bytes. Now, I have problems to trim this data string to what actually is necessary.

 

The real question is HOW are you communicating with this serial device....there's apparently a bug in the way you're receiving the data....

 

The first weighing gave you 22 of the same number

The second weighing gave your 34

and the third is either 52 53.

 

I don't see the pattern....this seems like a bug.  What code are you using to retrieve the string

EDIT: Nevermind, I didn't even notice the above post with the code in it.

 

 

What does ser_read() do?  I've never seen that function before and it's not in the manual.

Link to comment
Share on other sites

It is actually the code from http://easyvitools.com/phpserial/index.html

 

Whatever the way of communication be, the main problem here is to format the incoming data from the weighing machine in a way we want.

 

Considering a random string, all I need is to format it in such a way to get the number in one decimal point which is at the end of the string.

 

Simple example

 

$string = "+0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0001.086 Lt +0001.086 Lt +0001.086 Lt +0001.086 Lt +0001.";

 

In the above string, I need to format and get only the following output

 

1.0

 

Hope its clear now!?

Link to comment
Share on other sites

$string = "+0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0001.086 Lt +0001.086 Lt +0001.086 Lt +0001.086 Lt +0001.";

 

In the above string, I need to format and get only the following output

 

1.0

 

Hope its clear now!?

 

Don't you mean;

 

+2.2 Lt +2.2 Lt +2.2 Lt +2.2 Lt +2.2 Lt +2.2 Lt +2.2 Lt +2.2 Lt +1.1 Lt +1.1 Lt +1.1 Lt +1.1 Lt +1.0

 

??

Link to comment
Share on other sites

Don't you mean;

 

+2.2 Lt +2.2 Lt +2.2 Lt +2.2 Lt +2.2 Lt +2.2 Lt +2.2 Lt +2.2 Lt +1.1 Lt +1.1 Lt +1.1 Lt +1.1 Lt +1.0

 

??

 

Practically speaking, yes!

 

But the weighing machine is sending the output in that fashion. If it were to send the data only once, it should have looked like "+0002.189 Lt". But, it is sending the data to PHP continuously, so the same value has been repeated many number of times.

 

Further, when I change the weight on the weighing machine and refresh the PHP page to read again, it is adding the new weight to the end of the previous output.

 

So, I should be reading from the end of the string to get the recent weight. And the output data will be in the below format

 

+0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0001.086 Lt +0001.086 Lt +0001.086 Lt +0001.086 Lt +0001.
Link to comment
Share on other sites

Here's one way of getting it:

 

<?php
$str = '+0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0002.189 Lt +0001.086 Lt +0001.086 Lt +0001.086 Lt +0001.086 Lt +0001.';
preg_match('~[0-9]\.[0-9]+~', strrev($str), $match);
$value = number_format((float) strrev($match[0]), 1, '.', '');
echo $value;
?>

 

It reverses $str and matches a single digit followed by a dot and 1 or more digits (e.g. 0001.0 reversed). It then formats the number, to remove any leading zeros and always retain the single decimal.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.