Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove every Nth letter (loop)

Tags:

arrays

loops

php

I found here many responses for my question but I cat't find exactly what I am looking for.
I have to remove every 4th digit from an array, but beginning and the end making a circle, so If I remove 4th digit in next loop It's gonna be another digit (maybe 4th maybe 3rd) It's depend how many digit we have in string

$string = "456345673474562653265326";
$chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
$result = array();
for ($i = 0; $i < $size; $i += 4) 
{
    $result[] = $chars[$i];
}
like image 307
ssuperczynski Avatar asked Feb 28 '26 20:02

ssuperczynski


1 Answers

You could try doing this with preg_replace:

$string = "12345678901234567890";
$result = preg_replace("/(.{3})\d/", "$1", $string);
like image 175
zysoft Avatar answered Mar 03 '26 08:03

zysoft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!