Jump to content

Splitting string into three parts


Mcod

Recommended Posts

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 :)

Link to comment
Share on other sites

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!

 

:)

Link to comment
Share on other sites

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.)

 

 

Link to comment
Share on other sites

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..  :psychic:

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...
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.