Mcod Posted January 25, 2012 Share Posted January 25, 2012 Hi there, I am looking for a solution to split a string like this: $string = "jim.h|1234567890123456|0"; so I get three values 1.) jim.h 2.) 1234567890123456 3.) 0 I also need to validate that all parts exist. Part 1 before the | can be any length and only contain a-z 0-9 the minus sign and a dot - no spaces or any other characters. Part 2 (between the first and second | separator) must be a 16 chars string which must be 0-9 and a-z Part 3 must be a number, either 1, 2 or 3 If all conditions are given, I would like to isert this into a mysql database like INSERT INTO employees (name,key,level) VALUES ('$part1','$part2','$part3') - I know how to do this - even if I suck at other things It's important that a record only gets insert if all of the above is correct. This bit of code is all about user errors and people trying to manipulate the submitted AJAX form, so I want to make sure only valid characters can be submitted and that all values are required. Thank you for your time reading this Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/ Share on other sites More sharing options...
AyKay47 Posted January 25, 2012 Share Posted January 25, 2012 if the string contains a delimeter where you want it to split, you can use explode, as for the regex, I'm sure playful will provide that below me. Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/#findComment-1311230 Share on other sites More sharing options...
ragax Posted January 25, 2012 Share Posted January 25, 2012 Hi MCod! Run this: Input: jim.h|1234567890123456|0 Code: <?php $regex=',([^|]*)\|([^|]*)\|(.*),'; $string='jim.h|1234567890123456|0'; $hit=preg_match($regex,$string,$part); if($hit) { echo "Part 1: "; if(isset($part[1])) echo $part[1]; else echo 'n/a'; echo '<br />'; echo "Part 2: "; if(isset($part[2])) echo $part[2]; else echo 'n/a'; echo '<br />'; echo "Part 3: "; if(isset($part[3])) echo $part[3]; else echo 'n/a'; echo '<br />'; } ?> Output: Part 1: jim.h Part 2: 1234567890123456 Part 3: 0 Then you can do all the tests you want on $part[1], $part[2] and $part[3]. Let me know if this works for you! Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/#findComment-1311231 Share on other sites More sharing options...
ragax Posted January 25, 2012 Share Posted January 25, 2012 Ah, just saw AyKay47's post... He's so right, explode() or preg_split() is a great way to do it, and right again, I posted a regex 2 minutes after his message! I envy your psychic powers, AyKay. (And the clarity of mind to go to the easiest solution first.) Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/#findComment-1311233 Share on other sites More sharing options...
AyKay47 Posted January 25, 2012 Share Posted January 25, 2012 Ah, just saw AyKay47's post... He's so right, explode() or preg_split() is a great way to do it, and right again, I posted a regex 2 minutes after his message! I envy your psychic powers, AyKay. (And the clarity of mind to go to the easiest solution first.) well i saw that you were in the thread and I knew you would answer.. my mind likes easy things.. yes the easiest first step would be to validate the string using regex, then split the string by the delimiter using explode preg_split is also a viable approach. Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/#findComment-1311236 Share on other sites More sharing options...
ragax Posted January 25, 2012 Share Posted January 25, 2012 well i saw that you were in the thread and I knew you would answer Aha... so it's more like a magic trick than pure psychic ability??? Thank you for explaining your art to your public! ;-) Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/#findComment-1311239 Share on other sites More sharing options...
Pikachu2000 Posted January 26, 2012 Share Posted January 26, 2012 It needs to be asked: where are these values coming from, and how are they being concatenated into the resulting pipe delimited string? Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/#findComment-1311298 Share on other sites More sharing options...
Adam Posted January 27, 2012 Share Posted January 27, 2012 The regex provided in playful's post doesn't actually validate against the specific data requirements you mentioned. Replace it with: /^[a-z0-9\.-]|[a-z0-9]{16}|[1-3]$/ Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/#findComment-1311735 Share on other sites More sharing options...
ragax Posted January 27, 2012 Share Posted January 27, 2012 The regex provided in playful's post doesn't actually validate against the specific data requirements That's true: as I mentioned in the post, I just gave MCod part 1, part 2, and part 3 so he could do independent tests on these variables. (And potentially report to the person who submitted the data that one particular part is broken.) The post by AyKay discussed doing the same faster by using explode(). Adam is quite right that you can validate the entire string in one go: valid AND valid AND valid. If it fails, you don't know where, so it's up to you to choose the approach that works best for your needs. Nothing wrong with Adam's approach! MCod, small suggestions if you're going with Adam's expression: 1. You don't need the \ in the first bracket in front of the dot (\.) 2. You still need parentheses to capture the three parts since you said you wanted to split the string: INSERT INTO employees (name,key,level) VALUES ('$part1','$part2','$part3') 3. You said you want the last part to be 1, 2 or 3, but in the example you gave, $part3 was 0, so you may want to refine that in the last part of the regex, currently [1-3]. Wishing you all a fun weekend Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/#findComment-1311776 Share on other sites More sharing options...
.josh Posted January 27, 2012 Share Posted January 27, 2012 Just to clarify the above posts: $string = "jim.h|1234567890123456|1"; $regex = "~^([a-z0-9.-]+)\|([a-z0-9]{16})\|([1-3])$~i"; if ( preg_match($regex, $string, $part) ) { $sql = "INSERT INTO employees (name,key,level) VALUES ('{$part[1]}','{$part[2]}','{$part[3]}')"; } There are 2 things this regex does that was not explicitly defined: 1) you said the "name" can be any length. Well technically 0 is a length. I *assume* you don't *really* want to validate a missing name, so this regex validates on 1 or more chars. If you really don't care if a name is missing, change that "+" to a "*" in $regex. But on that note, are you sure that you *really* want to validate *any* length? What is your name column in your database set to? If it's only something like varchar(10) then it's either going to truncate or throw an error... 2) I *assumed* based on the context that it should be case-insensitive. If you really only want to accept lowercase values, remove that "i" on the end. of the $regex value. Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/#findComment-1311783 Share on other sites More sharing options...
Mcod Posted February 9, 2012 Author Share Posted February 9, 2012 Thanks for all your replies I managed to get it done, added some validation things and everything is set now Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/255789-splitting-string-into-three-parts/#findComment-1316183 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.