Q:

Write a PHP program to create a new string of the characters at indexes 0,1,4,5,8,9 ... from a given string

0

Write a PHP program to create a new string of the characters at indexes 0,1,4,5,8,9 ... from a given string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($s)
{
          $result = "";
            for ($i = 0; $i < strlen($s); $i=$i+4)
            {
                $c = $i + 2;
                $n = 0;
                $z = $c > strlen($s) ? 1 : 2;
                $n = $n + $z;
                $result = $result.substr($s, $i, $n);
            }
            return $result;
 }

echo (test("Python"))."\n";
echo (test("JavaScript"))."\n";
echo (test("HTML"))."\n";

Sample Output:

Pyon
JaScpt
HT

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now