1

This probably was asked millions of times, but I can't find a proper answer, really.

Basically, I have a huge .csv file to download data from and to insert it into a local one. It has 10 columns, and the third one is just a string, which always has the same "type" of string. something like: 123/45K67.

I just want to split it into three columns: 123, 45, K67 and exclude the slash totally.

Sorry for bad formatting, writing from mobile.

2
  • 1
    A simple way (if it's the same type of string) would be to regex it: preg_match("/(\d{3})\/(\d{2})(\w\d{2})/", "123/45K67", $matches); (Super basic example, you could clean the regex up) - Example Commented Feb 13, 2018 at 23:53
  • yh, I knew the wording will be bad from me. (argh! shift + enter sent the message too early). it is always like 123/45K67, but can be 111/40K60, etc. only constants are / and K here, length of the string is always the same and splits are always for 3 chars, 2 chars and 3 chars Commented Feb 13, 2018 at 23:58

1 Answer 1

1

One way would be:

$array = preg_split( "/(?=K)|\//", $value);

this returns:

array(3) { [0]=> string(3) "123" [1]=> string(2) "45" [2]=> string(3) "K67" }

using lookahead to include the matched character, here further information:

http://www.rexegg.com/regex-lookarounds.html

Sign up to request clarification or add additional context in comments.

1 Comment

hm, I think this is what I am looking for, but! what about including K letter in [2]? with it that's the perfect answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.