mangy1983 Posted September 22, 2011 Share Posted September 22, 2011 I have a variable that when echoed out displays the information from an email attachment (see below). My problem is although l know l should use preg replace l have absolutely no idea were to begin with this. I am looking for a snippet of code that will remove all information bar the decimal numbers. I have managed to remove all alphanumeric characters with preg replace but thats as far as l understand it. can anyone help me please? thanks Callum Below is the original attachment which consists of date and time in YYYYMMDDHHmmss format and the last 3 or four decimal numbers are of a rivers water level ## Exported ZRXP Block for Halkirk.SG.ir.O ##TSNAMEHalkirk.SG.ir.O;*; #ZRXPVERSION2206.235;*;ZRXPCREATORZEXP3.9.2;*; #ZRXPMODEextended;*; #CUNITm;*; #SNAMEHalkirk;*;SANR234231;*;SWATERThurso;*;CNR3033705;*; #CNAMESG;*;CMW86400;*;CTYPEn-min-ip;*; #RTYPEMomentanwerte;*;RTIMELVLHochaufloesend;*;RORPRoriginal;*;CNTYPE1;*; ##DAYSTART0000 #REXCHANGEHalkirk.SG.ir.O;*; #RSTATEW6;*; 20080224000000 0.48 0 20080224001500 0.477 0 20080224003000 0.477 0 20080224004500 0.475 0 20080224010000 0.477 0 20080224011500 0.475 0 20080224013000 0.476 0 20080224014500 0.473 0 20080224020000 0.471 0 20080224021500 0.469 0 20080224023000 0.469 0 20080224024500 0.466 0 20080224030000 0.466 0 20080224031500 0.464 0 20080224033000 0.463 0 20080224034500 0.461 0 20080224040000 0.458 0 20080224041500 0.458 0 20080224043000 0.456 0 20080224044500 0.456 0 20080224050000 0.457 0 20080224051500 0.454 0 20080224053000 0.455 0 20080224054500 0.452 0 20080224060000 0.452 0 20080224061500 0.452 0 20080224063000 0.451 0 20080224064500 0.452 0 20080224070000 0.451 0 20080224071500 0.451 0 20080224073000 0.449 0 20080224074500 0.451 0 20080224080000 0.448 0 20080224081500 0.447 0 20080224083000 0.449 0 20080224084500 0.448 0 20080224090000 0.448 0 20080224091500 0.445 0 20080224093000 0.444 0 20080224094500 0.446 0 20080224100000 0.448 0 20080224101500 0.448 0 20080224103000 0.449 0 20080224104500 0.45 0 20080224110000 0.448 0 20080224111500 0.448 0 20080224113000 0.449 0 20080224114500 0.449 0 20080224120000 0.451 0 20080224121500 0.449 0 20080224123000 0.45 0 20080224124500 0.449 0 20080224130000 0.45 0 20080224131500 0.448 0 20080224133000 0.452 0 20080224134500 0.452 0 20080224140000 0.454 0 20080224141500 0.456 0 20080224143000 0.455 0 20080224144500 0.459 0 20080224150000 0.457 0 20080224151500 0.456 0 20080224153000 0.454 0 20080224154500 0.457 0 20080224160000 0.457 0 20080224161500 0.458 0 20080224163000 0.461 0 20080224164500 0.458 0 20080224170000 0.458 0 20080224171500 0.46 0 20080224173000 0.46 0 20080224174500 0.458 0 20080224180000 0.462 0 20080224181500 0.462 0 20080224183000 0.459 0 20080224184500 0.461 0 20080224190000 0.459 0 20080224191500 0.456 0 20080224193000 0.453 0 20080224194500 0.448 0 20080224200000 0.446 0 20080224201500 0.443 0 20080224203000 0.44 0 20080224204500 0.439 0 20080224210000 0.431 0 20080224211500 0.428 0 20080224213000 0.423 0 20080224214500 0.42 0 20080224220000 0.416 0 20080224221500 0.414 0 20080224223000 0.41 0 20080224224500 0.407 0 20080224230000 0.406 0 20080224231500 0.403 0 20080224233000 0.401 0 20080224234500 0.399 0 20080225000000 0.397 0 20080225001500 0.394 0 20080225003000 0.393 0 20080225004500 0.392 0 20080225010000 0.391 0 20080225011500 0.39 0 20080225013000 0.389 0 20080225014500 0.389 0 20080225020000 0.388 0 20080225021500 0.388 0 20080225023000 0.39 0 20080225024500 0.39 0 20080225030000 0.39 0 20080225031500 0.389 0 20080225033000 0.391 0 20080225034500 0.391 0 20080225040000 0.39 0 20080225041500 0.39 0 20080225043000 0.39 0 20080225044500 0.39 0 20080225050000 0.39 0 20080225051500 0.388 0 20080225053000 0.388 0 20080225054500 0.388 0 20080225060000 0.388 0 20080225061500 0.389 0 20080225063000 0.388 0 20080225064500 0.387 0 20080225070000 0.387 0 20080225071500 0.387 0 20080225073000 0.387 0 20080225074500 0.386 0 20080225080000 0.387 0 20080225081500 0.386 0 20080225083000 0.386 0 20080225084500 0.386 0 20080225090000 0.386 0 20080225091500 0.384 0 20080225093000 0.386 0 20080225094500 0.386 0 20080225100000 0.386 0 20080225101500 0.386 0 20080225103000 0.388 0 20080225104500 0.387 0 20080225110000 0.387 0 20080225111500 0.387 0 20080225113000 0.387 0 20080225114500 0.387 0 20080225120000 0.387 0 20080225121500 0.386 0 20080225123000 0.386 0 20080225124500 0.387 0 20080225130000 0.383 0 20080225131500 0.383 0 20080225133000 0.383 0 20080225134500 0.383 0 20080225140000 0.382 0 20080225141500 0.382 0 20080225143000 0.382 0 20080225144500 0.383 0 20080225150000 0.382 0 20080225151500 0.382 0 20080225153000 0.379 0 20080225154500 0.38 0 20080225160000 0.379 0 20080225161500 0.38 0 20080225163000 0.379 0 20080225164500 0.378 0 20080225170000 0.383 0 20080225171500 0.379 0 20080225173000 0.383 0 20080225174500 0.382 0 20080225180000 0.382 0 20080225181500 0.382 0 20080225183000 0.382 0 20080225184500 0.382 0 20080225190000 0.382 0 20080225191500 0.38 0 20080225193000 0.383 0 20080225194500 0.382 0 20080225200000 0.383 0 20080225201500 0.383 0 20080225203000 0.384 0 20080225204500 0.386 0 20080225210000 0.391 0 20080225211500 0.395 0 20080225213000 0.398 0 20080225214500 0.406 0 20080225220000 0.412 0 20080225221500 0.418 0 20080225223000 0.426 0 20080225224500 0.43 0 20080225230000 0.432 0 20080225231500 0.432 0 20080225233000 0.436 0 20080225234500 0.439 0 20080226000000 0.44 0 20080226001500 0.44 0 20080226003000 0.443 0 20080226004500 0.441 0 20080226010000 0.443 0 20080226011500 0.443 0 20080226013000 0.444 0 20080226014500 0.443 0 20080226020000 0.442 0 20080226021500 0.443 0 20080226023000 0.443 0 20080226024500 0.44 0 20080226030000 0.442 0 20080226031500 0.44 0 20080226033000 0.44 0 20080226034500 0.436 0 20080226040000 0.437 0 20080226041500 0.435 0 20080226043000 0.436 0 20080226044500 0.436 0 20080226050000 0.432 0 20080226051500 0.434 0 20080226053000 0.436 0 20080226054500 0.435 0 20080226060000 0.434 0 20080226061500 0.435 0 20080226063000 0.435 0 the beginning of the echoed information and the end of the echoed information Quote Link to comment Share on other sites More sharing options...
cs.punk Posted September 22, 2011 Share Posted September 22, 2011 What exactly is this email attachment? What values are you trying to get? 0.48 0.477 0.477 ? Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 22, 2011 Share Posted September 22, 2011 Just a comment, but it is not necessary to add ALL that content. Just enough for us to understand the problem. Having said that, I think you are taking the wrong approach. My understanding is that you want the decimal numbers from that content, correct? Instead of removing all the other content using preg_replace() I would instead use preg_match_all() to find all the decimal numbers in that text. preg_match_all("#\d+\.\d+#", $emailInput, $matches); You will now have an array ($matches) that contains all the decimal numbers from the email input. However, I see there are some decimal numbers in the header text at the top of that text. I *assume* you only want the decimals that are in the lines such as 20080224000000 0.48 0. If that is true, then you can get only those by adding some criteria to look for a space before the decimal number. preg_match_all("# (\d+\.\d+)#", $input, $matches); Note: this second solution will create a mulch-dimensional array that includes two sets of all matches. The first one (index 0) contains the matches with the leading space and the second (index 1) contains only the decimal matches. Just to be clear, if the input contains these lines: 20080224000000 0.48 0 20080224001500 0.477 0 20080224003000 0.477 0 20080224004500 0.475 0 The regex above would return: 0.48, 0.477, 0.477 & 0.475 Quote Link to comment Share on other sites More sharing options...
mangy1983 Posted September 22, 2011 Author Share Posted September 22, 2011 What exactly is this email attachment? What values are you trying to get? 0.48 0.477 0.477 ? Yes that is exactly the values l am trying to get. I am looking to strip these values and add them into an array so that the last of the values (the latest) can be added to a database and from there displayed on my website thanks yo for your reply cheers Callum Quote Link to comment Share on other sites More sharing options...
mangy1983 Posted September 22, 2011 Author Share Posted September 22, 2011 Just a comment, but it is not necessary to add ALL that content. Just enough for us to understand the problem. Having said that, I think you are taking the wrong approach. My understanding is that you want the decimal numbers from that content, correct? Instead of removing all the other content using preg_replace() I would instead use preg_match_all() to find all the decimal numbers in that text. preg_match_all("#\d+\.\d+#", $emailInput, $matches); You will now have an array ($matches) that contains all the decimal numbers from the email input. However, I see there are some decimal numbers in the header text at the top of that text. I *assume* you only want the decimals that are in the lines such as 20080224000000 0.48 0. If that is true, then you can get only those by adding some criteria to look for a space before the decimal number. preg_match_all("# (\d+\.\d+)#", $input, $matches); Note: this second solution will create a mulch-dimensional array that includes two sets of all matches. The first one (index 0) contains the matches with the leading space and the second (index 1) contains only the decimal matches. Just to be clear, if the input contains these lines: 20080224000000 0.48 0 20080224001500 0.477 0 20080224003000 0.477 0 20080224004500 0.475 0 The regex above would return: 0.48, 0.477, 0.477 & 0.475 I am just looking to strip the last decimal number (the latest) in order to save it to a database and therefore display the value on my website. thank you for your help cheers Callum Quote Link to comment Share on other sites More sharing options...
mangy1983 Posted September 22, 2011 Author Share Posted September 22, 2011 Just a comment, but it is not necessary to add ALL that content. Just enough for us to understand the problem. Having said that, I think you are taking the wrong approach. My understanding is that you want the decimal numbers from that content, correct? Instead of removing all the other content using preg_replace() I would instead use preg_match_all() to find all the decimal numbers in that text. preg_match_all("#\d+\.\d+#", $emailInput, $matches); You will now have an array ($matches) that contains all the decimal numbers from the email input. However, I see there are some decimal numbers in the header text at the top of that text. I *assume* you only want the decimals that are in the lines such as 20080224000000 0.48 0. If that is true, then you can get only those by adding some criteria to look for a space before the decimal number. preg_match_all("# (\d+\.\d+)#", $input, $matches); Note: this second solution will create a mulch-dimensional array that includes two sets of all matches. The first one (index 0) contains the matches with the leading space and the second (index 1) contains only the decimal matches. Just to be clear, if the input contains these lines: 20080224000000 0.48 0 20080224001500 0.477 0 20080224003000 0.477 0 20080224004500 0.475 0 The regex above would return: 0.48, 0.477, 0.477 & 0.475 edited to say l figured my problem out with your code and l must say you sir are a genius! cheers Callum Quote Link to comment Share on other sites More sharing options...
mangy1983 Posted September 22, 2011 Author Share Posted September 22, 2011 I'm nearly there now. I just need to know how l can automatically have it display the highest number of the second column (last row) inside the array as with the code l have used (thanks again mjdamato) l have found that the array row containing the data l require is in array row $matches[0][220]. I am just future proofing the script as l am guessing that l will be sent variable amounts of rows in the attachments so having a set row number ie 220 in this case will not always work. thanks Callum Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 22, 2011 Share Posted September 22, 2011 I'm sorry, but I'm not sure if you are asking for the "last" value or the "highest" value. I know that in the data you presented that the last value is also the highest value, but it is easy enough to code for either the last value or the highest value - so you need to decide. You really should start looking through the manual or do some google searching as either of these solutions are the simple implementation of a single function. To get the last value: $lastValue = array_pop($matches[1]); To get the highest value: $maximumValue = max($matches[1]); Quote Link to comment Share on other sites More sharing options...
mangy1983 Posted September 24, 2011 Author Share Posted September 24, 2011 Thanks again mjdamato. Your information once again was invaluable. cheers Callum Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.