user2117995
user2117995

Reputation: 1

Select first or last character groups from string with period separators

I am asking help with php code to manipulate a string that I am retrieving from an SQL database. The string is in this format: Groups of 3-5 alphanumerics separated by periods. The number of alphanumeric groups in the string is quite variable (from 1 - 20 or more groups).

I need to do 2 things with these:

  1. separate into a new string just the first 3 groups between periods (in Example 1, results would be "J89.NEWTT.IIU")
  2. separate into a new string just the last 3 groups between periods (in Example 2, results would be "W9700.NUGSM.N6500")

I'm having trouble getting the usual players to work with this. Thanks for any help!

Upvotes: 0

Views: 225

Answers (1)

Eevee
Eevee

Reputation: 48546

Split it (explode), slice out the parts you want, then join them back together with . again.

This is a strange format and picking the first/last three chunks seems really arbitrary. Is it structured data? If so, why are you storing it in a single field in your database?

Upvotes: 1

Related Questions