Re: [PHP] how to parse a string parse with ereg
On Friday 19 November 2004 01:50, [EMAIL PROTECTED] wrote: Then you can process each line separately but in groups 3 lines at a time (assuming they're supposed to be groups of 3). but what happen's in the case a user drop down every line in a web form without a ELO character ? Not quite sure what you mean here. In a textarea pressing ENTER/RETURN will generate an EOL character. If they have a habit of not pressing ENTER/RETURN when they should then you can run a simple check on the length of a line and reject and/or ask them to enter again. In any case if you want to force the user to enter lines separately use a bunch of input type=text ... instead of textarea. The point here is that doing relatively simple checks early on in the data entry stage to ensure that you get sane data means you don't have to trawl through garbage in the data processing stage trying to figure out all the ways in which a user could have entered incorrect data. If you state clearly, in detail, what exactly you want the regex to match then maybe someone could construct a better one for you. Ok, at the moment I verify the assuming missed device with a difference of the following both block's: [...] You're telling us what you're *doing*, not what you *want* your code to do. We need to know what you *want* your code to do before we can suggest improvements or whatever. Anyway here's a PCRE to match a single IP address: (([0-9])|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([1-9] [0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([1-9][0-9])|(1[0-9] [0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([1-9][0-9])|(1[0-9][0-9])|(2[0-4] [0-9])|(25[0-5])) It will validate an IP address of the form: (0-255).(0-255).(0-255).(0-255) No leading zeros allowed. ? $INET_NUM_exp = ereg_replace([\ ]+([a-z]+)([0-9]{1}),\\1\\2_XrR,$INET_NUM); $sorter = explode(_XrR,$INET_NUM_exp); $result = count($sorter); $result = $result-1; Instead of doing this bit of code (which I don't understand), I suggest that you incorporate some simple check for interface into your regex: (((eth|ppp)\d{1,2})|lo) -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* Bloom's Seventh Law of Litigation: The judge's jokes are always funny. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to parse a string parse with ereg
Helo, I meant explode each *line* (at the EOL character(s)), so this: xxx.x.x.x | 0.0.0.0 255.255.255.255 UH 0 | | | 0 | | | | 0 ppp0 is an element of the array. And this: 192.168.0.0 0.0.0.0 255.255.255.0 U | | 0 | | | 0 | | | | 0 eth1 is another element, etc. 0.0.0.0 | | xxx.x.x.x 0.0.0.0 | | UG | 0 | | | 0 | | | | 0 E.g. Line 3 won't be in an array. This could maybe be caused from drop paste to copy from a shell into the browser while using an extraordinary OS. Then you can process each line separately but in groups 3 lines at a time (assuming they're supposed to be groups of 3). but what happen's in the case a user drop down every line in a web form without a ELO character ? The difference lead's now to an error as the syntax is wrong. If you state clearly, in detail, what exactly you want the regex to match then maybe someone could construct a better one for you. Ok, at the moment I verify the assuming missed device with a difference of the following both block's: ? $INET_NUM_exp = ereg_replace([\ ]+([a-z]+)([0-9]{1}),\\1\\2_XrR,$INET_NUM); $sorter = explode(_XrR,$INET_NUM_exp); $result = count($sorter); $result = $result-1; $INET_NUM_exp2 = ereg_replace(([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[\ ]+([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[\ ]+([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[\ ]+,\\1\\2_XrR,$INET_NUM); $sorter2 = explode(_XrR,$INET_NUM_exp2); $result2 = count($sorter2); $result2 = $result2-1; ? In the case $result is equal to $result2 the following code will be called within a function. If not an error occurs which will lead to a exit before the following is done. ? if ($INET_NUM ereg('[\ ]+([a-z]+)([0-9]{1})',$INET_NUM,$part)) { $INET_STRING = $part[1].$part[2]; $INET_NUM = ereg_replace([\ ]+([a-z]+)([0-9]{1}),\\1\\2_XrR,$INET_NUM); $sorter = explode(_XrR,$INET_NUM); array_pop($sorter); while(list($key,$value) = each($sorter)) { if (ereg('([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[\ ]+([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[\ ]+([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[\ ]+([A-Z]+)[\ ]+([0-9]+)[\ ]+([0-9]+)[\ ]+([0-9]+)', $value, $part)) { if ($part[1] = 255 $part[2] = 255 $part[3] = 255 $part[4] = 255 $part[5] = 255 $part[6] = 255 $part[7] = 255 $part[8] = 255 $part[9] = 255 $part[10] = 255 $part[11] = 255 $part[12] = 255 is_numeric($part[1]) is_numeric($part[2]) is_numeric($part[3]) is_numeric($part[4]) is_numeric($part[5]) is_numeric($part[6]) is_numeric($part[7]) is_numeric($part[8]) is_numeric($part[9]) is_numeric($part[10]) is_numeric($part[11]) is_numeric($part[12])){ $value = clean_sql_output($value); $value = str_replace(\\r\\n,br,$value); $INET_NUM_array[$i] = $value; $i++; } else { $stat_ip_address_is_not_valid++; } } else { $stat_ip_address_is_not_valid++; } } } else { $stat_ip_address_is_not_valid++; } if ($stat_ip_address_is_not_valid1) { return $INET_NUM_array;} $INET_NUM_array[0] = 0; return $INET_NUM_array; ### SYNTAX ERROR ## } ? -- Best Regards, Mark -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to parse a string parse with ereg
Hi, I have to admit I'm not following you. My suggestion that you use explode() was based on the premise that you have a bunch of lines: ... xxx.x.x.x 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 0.0.0.0 xxx.x.x.x 0.0.0.0 UG 0 0 0 ppp0 ... that are currently stored in a string. After explode() you can use foreach() to loop through the array and check each line individually. Then based on whether the line passes the check you could either: a) unset that array element (doesn't pass check so get rid of it) or b) copy that element to a new array (passes check so store it in a 'good' array) Either way you'll end up with an array containing only 'good' lines. Well this work's fine in the case the user input is correct :-) But it's not required to proof in the case I would assume that a user input is correct. To explode with the device as line break to allocate an array mem lead's not to an error as this line it won't be in the array. xxx.x.x.x | 0.0.0.0 255.255.255.255 UH 0 | | | 0 | | | | 0 ppp0 192.168.0.0 0.0.0.0 255.255.255.0 U | | 0 | | | 0 | | | | 0 eth1 0.0.0.0 | | xxx.x.x.x 0.0.0.0 | | UG | 0 | | | 0 | | | | 0 E.g. Line 3 won't be in an array. This could maybe be caused from drop paste to copy from a shell into the browser while using an extraordinary OS. *** The only solution which should be better now for me is to proof for 3 ip addresses in a row to be able to count how much of this matching construct in a result is as basis for a difference. Then I'am able to compare this with matching lines where the device has been taken to explode. The difference lead's now to an error as the syntax is wrong. The only problem which still remains is the case the device is missing __AND__ there're not 3 following ip addresses. This user input data is not going to be proofed but it seems to be better than before. And for sure normally the user input should be correct :-) -- Best Regards, Mark -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to parse a string parse with ereg
On Thursday 18 November 2004 06:51, [EMAIL PROTECTED] wrote: Well this work's fine in the case the user input is correct :-) But it's not required to proof in the case I would assume that a user input is correct. To explode with the device as line break to allocate an array mem lead's not to an error as this line it won't be in the array. I meant explode each *line* (at the EOL character(s)), so this: xxx.x.x.x | 0.0.0.0 255.255.255.255 UH 0 | | | 0 | | | | 0 ppp0 is an element of the array. And this: 192.168.0.0 0.0.0.0 255.255.255.0 U | | 0 | | | 0 | | | | 0 eth1 is another element, etc. 0.0.0.0 | | xxx.x.x.x 0.0.0.0 | | UG | 0 | | | 0 | | | | 0 E.g. Line 3 won't be in an array. This could maybe be caused from drop paste to copy from a shell into the browser while using an extraordinary OS. Then you can process each line separately but in groups 3 lines at a time (assuming they're supposed to be groups of 3). *** The only solution which should be better now for me is to proof for 3 ip addresses in a row to be able to count how much of this matching construct in a result is as basis for a difference. Then I'am able to compare this with matching lines where the device has been taken to explode. The difference lead's now to an error as the syntax is wrong. If you state clearly, in detail, what exactly you want the regex to match then maybe someone could construct a better one for you. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* * Knghtbrd crosses his toes Knghtbrd (if I crossed my fingers it would be hard to type) */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to parse a string parse with ereg
Hi Jason Wong, The regular expression is already working but how can I reduce the string to have then only 2 lines ? Convert the string into an array using explode(). Thank's a lot for this information. I tried this but one thing is not suitable so far for me while using explode. E.g. if have the output of route -n: xxx.x.x.x 0.0.0.0 255.255.255.255 UH 0 00 ppp0 192.168.0.0 0.0.0.0 255.255.255.0 U0 00 eth1 0.0.0.0 xxx.x.x.x 0.0.0.0 UG 0 00 ppp0 By using this example it's quite easy to analyze all lines due to a correct syntax: $INET_NUM = ereg_replace([\ ]+([a-z]+)([0-9]{1}),\\1\\2_XrR,$INET_NUM); $sorter = explode(_XrR,$INET_NUM); But it has one weakness. In the case the device (ppp0) is missing somewhere in the output as the user didn't intend to integrate this required information, and by having insight it could be more than 10 lines with 10.000 rules of netfilter, explode will show concerning this example of 3 lines 2 defined arrays and it won't lead to an error as explode doesn't give a return value back which has not been proofed to be exploded with _XrR. As I want to proof the syntax of all lines this is wrong and not usable. What I would need is a function which counts, based on a regular expression, how many times this shema occurs inside of a string. So I can reduce the possibilty to have not an error and I could proof against the missing device by searching for 3 ip addresses followed by spaces and so on. E.g. the above example of explode would show 2 set arrays but with a regular expression which counts all ip addresses and so on it would show 3 lines matching the construct. The difference would lead then to an error message as the user input was wrong. -- Best Regards, Mark. Hello, I am brand new to meditation, and I have a frustrating habit of falling asleep in class. I don't know how to stop this. When my teacher tells us to relax our bodies and focus on breathing, my body relaxes, but so does my brain. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to parse a string parse with ereg
On Wednesday 17 November 2004 07:17, [EMAIL PROTECTED] wrote: But it has one weakness. In the case the device (ppp0) is missing somewhere in the output as the user didn't intend to integrate this required information, and by having insight it could be more than 10 lines with 10.000 rules of netfilter, explode will show concerning this example of 3 lines 2 defined arrays and it won't lead to an error as explode doesn't give a return value back which has not been proofed to be exploded with _XrR. As I want to proof the syntax of all lines this is wrong and not usable. I have to admit I'm not following you. My suggestion that you use explode() was based on the premise that you have a bunch of lines: ... xxx.x.x.x 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 0.0.0.0 xxx.x.x.x 0.0.0.0 UG 0 0 0 ppp0 ... that are currently stored in a string. After explode() you can use foreach() to loop through the array and check each line individually. Then based on whether the line passes the check you could either: a) unset that array element (doesn't pass check so get rid of it) or b) copy that element to a new array (passes check so store it in a 'good' array) Either way you'll end up with an array containing only 'good' lines. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* Microsoft broke Volkswagen's world record: Volkswagen only made 22 million bugs! */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how to parse a string parse with ereg
On Friday 12 November 2004 00:50, [EMAIL PROTECTED] wrote: The regular expression is already working but how can I reduce the string to have then only 2 lines ? Convert the string into an array using explode(). -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* I believe that professional wrestling is clean and everything else in the world is fixed. -- Frank Deford, sports writer */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php