1

in order to print a code128 barcode with a zebra printer in ZPL II language, i'm trying to convert a string (which is my barcode) into a new string. This new string is the same string with some specific commands related to switching between ALPHA and NUMERIC modes. Switching to NUMERIC mode helps making your barcode more compact. so lets say the barcode i want to print is : C00J025042101110823611001150119611 the result should be this :

>:C00J>5025042101110823611001150119611

>: mean we Start in ALPHA
>5 Mean we switch from ALPHA to NUMERIC ONLY
>6 Mean we switch from NUMERIC to ALPHA

So what i'm looking for is (if possible) a REGEX which will insert >5 or >6 in my string.

here is another example:

barcode to print = CJYJY10442101110S23611001150119611

String to send to printer = >:CJYJY1>50442101110>6S2>53611001150119611

Some more example, in order to understand how it starts. On the left the barcode to print, on the right the code sent to the printer.

C000025042101110823611001150119611 >:C0>500025042101110823611001150119611 CJ00025042101110823611001150119611 >:CJ>500025042101110823611001150119611 C0J0025042101110823611001150119611 >:C0J0>5025042101110823611001150119611 C00J025042101110823611001150119611 >:C00J>5025042101110823611001150119611 C000J25042101110823611001150119611 >:C000J2>55042101110823611001150119611 C0000J5042101110823611001150119611 >:C>50000>6J>55042101110823611001150119611 C00000J042101110823611001150119611 >:C0>50000>6J0>542101110823611001150119611

Extra note from the ZEBRA ZPL II documentation:

Code 128 subsets A and C are programmed as pairs of digits, 00-99, in the field data string. [...] in subset C, they are printed as entered. NOTE: Non-integers programmed as the first character of a digit pair (D2) are ignored. However, non-integers programmed as the second character of a digit pair (2D) invalidate the entire digit pair, and the pair is ignored. An extra, unpaired digit in the field data string just before a code shift is also ignored.

Subset C is NUMERIC, invoked by ">6"

cetipabo
  • 453
  • 5
  • 12
  • From what i know there is no criteria, it's up to you to choose when you want to switch to ALPHA or NUMERIC. ZPL-II langage is not simple i wanted to be clear enough. it is possible to stay in ALPHA from the begining and up to the end of the string, but it makes the barcode very large. switching to NUMERIC mode is a trick to reduce the barcode size a lot. – cetipabo Jun 12 '19 at 15:20
  • you are right, this was the string generated by my label designer software (Codesoft), i don't know why it didn't do it that way...but anyway it should work. btw here is a picture with the barcodein Alpha only and below the same barcode switching from alpha to NUM, you can see the width difference : https://i.imgur.com/SvDpZ2D.png – cetipabo Jun 12 '19 at 15:31

1 Answers1

3

You can use preg_replace with array arguments:

$result = preg_replace(
    array(
        '/(^\D)/',
        '/(\D)(\d)/',
        '/(\d)(\D)/',
    ),
    array(
        '>:$1',
        '$1>5$2',
        '$1>6$2',
    ),
    $code
);

UPD

According to the last comments you can try to switch between modes only if pair numbers found.

$result = preg_replace(
    array(
        '/(^\D)/',
        '/((?:\d{2})+)/',
        '/\>[56]$/',
    ),
    array(
        '>:$1',
        '>5$1>6',
        '',
    ),
    $code
);
Vitalii
  • 1,137
  • 14
  • 25
  • this gives me an error : Parse error: syntax error, unexpected '[', expecting ')' – cetipabo Jun 12 '19 at 15:40
  • Which version of PHP do you running? – Vitalii Jun 12 '19 at 15:43
  • i'm running PHP 5.3.13 on my server...for some reason i can't upgrade it for the moment. – cetipabo Jun 12 '19 at 15:44
  • Have just updated code snipped according to PHP version < 5.4. In PHP 5.4 you can repace `array()` with `[]` – Vitalii Jun 12 '19 at 15:45
  • Well, when i send to the printer the resulted code (which is 100% correct) the last number on the right is missing when i read the barcode with my scanner... i don't know if it's because we do this `>:C>500J>50250....` instead of this `>:C00J>50250....` – cetipabo Jun 12 '19 at 16:20
  • Сan you please provide me with alpha mode restrictions if any. – Vitalii Jun 12 '19 at 16:27
  • omg that's not very simple, the documentation about this is here : https://support.zebra.com/cpws/docs/zpl/code_128.htm#Sample%20ZPL%20switching%20from%20Subset%20C%20to%20B%20to%20A the switch between ALPHA/NUM can be seen in that picture : https://support.zebra.com/cpws/docs/images/128_001.gif i'm wondering if it's something about "pairs"...the strings have to be pairs and not odd when converting ? i have no clue about how it is working... – cetipabo Jun 12 '19 at 16:34
  • Well, so using my label software i generated some barcodes and captured the results. step by step i moved to the right the first Letter to see what is the rule..i edited my question and added the examples. – cetipabo Jun 12 '19 at 16:52
  • Well, so this is what is actually happening to me, it is explained in a note in my link above : "Code 128 subsets A and C are programmed as pairs of digits, 00-99, in the field data string. [...] in subset C, they are printed as entered. NOTE: Non-integers programmed as the first character of a digit pair (D2) are ignored. However, non-integers programmed as the second character of a digit pair (2D) invalidate the entire digit pair, and the pair is ignored. An extra, unpaired digit in the field data string just before a code shift is also ignored." – cetipabo Jun 13 '19 at 09:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194866/discussion-between-cetipabo-and-vitaliy-krushelnitsky). – cetipabo Jun 13 '19 at 09:41
  • Vitaliy, your update is 100% working. you are genius, thank you very very much for your help. you just missed a single quote in the first array. – cetipabo Jun 13 '19 at 11:42