Q:

Write a PHP program to create a new string from a given string. If the first or first two characters is 'a'

0

Write a PHP program to create a new string from a given string. If the first or first two characters is 'a', return the string without those 'a' characters otherwise return the original given string.

All Answers

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

<?php
function test($s1)
 { 
            if (strlen($s1) == 1 && substr($s1, 0, 1) == "a")
            {
                $s1 = substr($s1, 1);
            }

            if (strlen($s1) > 1)
            {
                if (substr($s1, 1, 1) == "a")
                {
                    $s1 = substr($s1, 0, 1).substr($s1, 2);
                }

                if (substr($s1, 0, 1) == "a")
                {
                    $s1 = substr($s1, 1);
                }
            }

            return $s1;
 }

echo test("abcab") . "\n";
echo test("Python") . "\n";
echo test("aacda") . "\n";
echo test("jython") . "\n";
Sample Output:
bcab
Python
cda
jython

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