Split PHP String

Asked 1 months ago, Updated 1 months ago, 2 views

In PHP, click
Certain variables $hoge
Let's say there is.

$hoge,
I'd like to divide it into one or two

1 is 32 characters from the beginning of the $hoge,
2 is the remainder of 1 with a maximum of 38 characters

How should I write it to split it into?

Thank you for your cooperation

php

2022-09-30 14:18

2 Answers

For example, using preg_match() as a separate solution.

preg_match('/^(.{32})(.{0,38})/',
    'abcdefghijklmnopqrstuvwxyz0123456789',
    $match);

echo "$match[1]\n";
echo "$match[2]\n";
=>
abcdefghijklmnopqrstuvwxyz012345
6789


2022-09-30 14:18

If it's simple, it's like this

//From the beginning (0th character) to the 32nd character
$a1 = substr($hoge,0,32);
// 33 characters to the end
$a2 = substr($hoge,33);

If you want to arrange it, maybe this is it

//Array 32 characters apart
$arr = str_split($hoge,32);
// Cutting the leading element
$a1 = array_shift($arr);
// Residual coupling
$a2 =implode($arr);


2022-09-30 14:18

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.