Jump to content

Recommended Posts

Hi guy,

 

I have this combination of record number, ID and qty and I need help sort them out and fill in column as follow:

 

(141442872453(2)) NV4852-SD (2)

 

I need to get the following:

 

record number: "141442872453"

 

ID #: "NV4852-SD"

 

QTY: "2"

 

Thanks

 

 

 

 

 

 

Tis better that you read the manual and Learn from your trial and error(s) than for me (or someone else here) writing it for you.

 

Good luck and have fun. Besides if I show you how to separate out your 3 values from this string what the heck are you going to then? Have someone write the next piece of the puzzle for you? C'mon!

  • Like 1

I don't think this is a scenario where I would use explode. Rather, I think, regular expression would be a better solution. Having said that, you show a sample string that contains 4 pieces of information, yet you only show three values. Do the two instances of "2" in the string both represent the quantity?

Alternative to regex

$a = '(141442872453(2)) NV4852-SD (2)';
$b = str_replace(['(',')'], ' ', $a);
$data = sscanf($b, ' %s %d  %s %d');

echo '<pre>',print_r($data, true),'</pre>';

giving

Array
(
    [0] => 141442872453
    [1] => 2
    [2] => NV4852-SD
    [3] => 2
)

Barand's solution is the most elegant and I like it a lot! But if you are a newbie, then the explode function is nice and simple, provided that the three values in your string are always separated by spaces.

If that is the case, then

 

 

$string =  '(141442872453(2)) NV4852-SD (2)';     
$parts = explode(' ', $string);
     

 

Will split the string wherever there is a space and put the parts into an array called $parts

so :

$parts[0] = '(141442872453(2))'

$parts[1] = 'NV4852-SD'

$parts[2] = '(2)'

 

The ID number is just $parts[1]

To get the record number and quantity, you need to do a little more work. For that, you could try a function called substr ... like the others, I'll leave you now to look that one up in the manual and see if you can take it forward yourself. Good luck !

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.