Thursday, 15 September 2011

PHP: replace 3 characters every 4th character in string -



PHP: replace 3 characters every 4th character in string -

i have string , want know how can maintain 1 character in 4 character interval.

$string = "bc7yad3odf0x"; // , goes this..

i want new string be:

yox

which comes out of bc7 y ad3 o df0 x

the 3 characters ommited char-char-number.

edit: give thanks answering. tested answers , work fine me, except trisweb's preg_replace() solution, "unknown modifier 'g'". take bhavik shah's reply since think simplest understanding.

$nstr = ""; $str_array = array(); //split string in chunks of 4-char strings $str_array = str_split($string, 4); //loop through elements of array , take required character foreach($str_array $key => $value){ $nstr .= $value[3]; } echo $nstr;

more details str_split.

php string replace intervals

No comments:

Post a Comment