Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ilhamzacky's avatar

While Converting Python To PHP Return Empty

need a small help, I have a python script, Can I convert this code to PHP, is there any plugins or tools? can someone please help me to covert this python code to PHP (Laravel) I tried this way but didn't work

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

$process = new Process(['python', '/path/to/your_script.py']);
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

echo $process->getOutput();

Here is my script

lis = ['E', 'P', 'S', 'C', 'F']
bc_ids = []
for x in lis:
    bc_id = list(range(0, 5))
    bc_id = [x + 'XX-' + format(y,'04d') for y in bc_id]
    bc_ids = bc_ids + bc_id
​
raw_ids = bc_ids
​
def convertIDToDigits(string):
    # strip off the first three characters and convert them to ascii
    # append the numbers to the remaining digits
    # strip out any spaces or dashes
    s = string.replace('-', '')
    digits = s[3:]
    chars = s[:3]
    # reverse the list so when we append the numbers we preserve the order
    chars = list(reversed(chars))
    for c in chars:
        num = ord(c.upper())
        digits = str(num) + digits
    return (digits)
​
def convertIDToVarChar(string):
    # re-assemble the ID back to the original format
    # take the first six digits
    digits = string[6:]
    d1 = digits[:4]
    d2 = digits[4:]
    chars = string[:6]
    c1 = chr(int(chars[:2]))
    c2 = chr(int(chars[2:4]))
    c3 = chr(int(chars[4:6]))
    return c1 + c2 + c3 + '-' + d1 + '-' + d2
​
# from https://github.com/mmcloughlin/luhn/blob/master/luhn.py
def checksum(string):
    """
    Compute the Luhn checksum for the provided string of digits. Note this
    assumes the check digit is in place.
    """
    digits = list(map(int, string))
    odd_sum = sum(digits[-1::-2])
    even_sum = sum([sum(divmod(2 * d, 10)) for d in digits[-2::-2]])
    return (odd_sum + even_sum) % 10
​
def verify(string):
    # if it contains only digits
    if not string.isdigit():
        string = convertIDToDigits(string)
    return (checksum(string) == 0)
​
def generate(string):
    cksum = checksum(string + '0')
    return (10 - cksum) % 10
​
def append(string):
    return string + str(generate(string))
​
computed_ids = []
​
#print(raw_ids)
​
for id in raw_ids:
    id = convertIDToDigits(id)
    new_id = convertIDToVarChar(append(id))
    computed_ids.append(new_id)
​
for id in computed_ids:
    print(id)

This script is used to get some random numbers. Thank You.

0 likes
0 replies

Please or to participate in this conversation.