Str_Replace Question - Replace Problem

The application has to do with an alarm system. When an alarm event occurs it generates a text file that has a 16 digit/character number at the bottom of the text file and logs it on my linux server. I am trying to translate the code into english using str_replace.

The problem i have in the first example below is the last character/number is a checksum and changes so my search never comes up with a match. I got around this by just seaching the first 15 digits/characters even though the string is 16 digits/characters. This works and i have a valid match. Problem is once the replace happens, the 16th digit/character is showing up to the right of the replace.

The problem i have in the second example below is only the first 10 digits are constant and the remaining 6 are never the same.

Question /Example 1: I can only search on the first 15 characters/digits of the 16 generated by the alarm as the 16th is never the same.

The alarm generates the following text file:

[metadata]

PROTOCOL=ADEMCO_CONTACT_ID
CALLINGFROM=5085551212
CALLERNAME=
TIMESTAMP=Wed Nov 21, 2012 @ 13:33:58 EST

[events]

769418130101000B

I execute the following php script:

[php]

<?php $file = '/etc/asterisk/alarm/latest.txt'; // // System Alarms file_put_contents($file,str_replace('7694181110','7694181110 Fire Alarm ',file_get_contents($file))); file_put_contents($file,str_replace('7694183110','7694183110 Fire Alarm Clear ',file_get_contents($file))); file_put_contents($file,str_replace('7694181111','7694181111 Smoke Alarm ',file_get_contents($file))); file_put_contents($file,str_replace('7694183111','7694183111 Smoke Alarm Clear ',file_get_contents($file))); file_put_contents($file,str_replace('7694181302','7694181302 Low System Battery ',file_get_contents($file))); file_put_contents($file,str_replace('769418130101000','769418130101000 AC Power Failure ',file_get_contents($file))); file_put_contents($file,str_replace('769418330101000','769418330101000 AC Power Restore ',file_get_contents($file))); ?>

[/php]

The output from the above line is:

769418130101000 AC Power Failure B

How do I stop the 16th character/digit in this case “B” from showing up in the output???

Question/Example 2: For some alarms I can only search on the first 10 characters/digits of the 16 generated by the alarm as the last 6 are never the same.

The alarm generates the following Text file:

[metadata]

PROTOCOL=ADEMCO_CONTACT_ID
CALLINGFROM=5085551212
CALLERNAME=
TIMESTAMP=Wed Nov 21, 2012 @ 13:33:58 EST

[events]

7694181302123456

[php]

<?php $file = '/etc/asterisk/alarm/latest.txt'; // // System Alarms file_put_contents($file,str_replace('7694181110','7694181110 Fire Alarm ',file_get_contents($file))); file_put_contents($file,str_replace('7694183110','7694183110 Fire Alarm Clear ',file_get_contents($file))); file_put_contents($file,str_replace('7694181111','7694181111 Smoke Alarm ',file_get_contents($file))); file_put_contents($file,str_replace('7694183111','7694183111 Smoke Alarm Clear ',file_get_contents($file))); file_put_contents($file,str_replace('7694181302','7694181302 Low System Battery ',file_get_contents($file))); file_put_contents($file,str_replace('769418130101000','769418130101000 AC Power Failure ',file_get_contents($file))); file_put_contents($file,str_replace('769418330101000','769418330101000 AC Power Restore ',file_get_contents($file))); ?>

[/php]

7694181302 Low System Battery 123456

How do i stop the last 6 digits/characters from showing up in the output?

The script needs to be looking for either scenerio
I am just learning php so please be as detailed as possible. Thanks very much for your help !!!

How is the file being parsed?

You could use preg_replace instead. If the codes are exactly as you say it would look something like this:

[php]
$contents = file_get_contents($file);
$contents = preg_replace(
/* matches /
array(
‘/(7694181110)([0-9]{6})/’,
‘/(7694183110)([0-9]{6})/’,
‘/(7694181111)([0-9]{6})/’,
‘/(7694183111)([0-9]{6})/’,
‘/(7694181302)([0-9]{6})/’,
‘/(769418130101000)([A-Za-z]{1})/’,
‘/(769418330101000)([A-Za-z]{1})/’
),
/
replacements */
array(
‘${1} Fire Alarm’,
‘${1} Fire Alarm Clear’,
‘${1} Smoke Alarm’,
‘${1} Smoke Alarm Clear’,
‘${1} Low System Battery’,
‘${1} AC Power Failure’,
‘${1} AC Power Restore’
),
$contents
);
file_put_contents($file, $contents);
[/php]

Basically what is happening is you are first matching the string that you want to keep ‘7694181110’ which is used as variable ${1} then matching the string you don’t want which is simply discarded.

[0-9]{6} (matches 6 digits)
[A-Za-z]{1} (matches 1 letter)

Thanks Very Much !!!

Sponsor our Newsletter | Privacy Policy | Terms of Service