Write a PHP program to create a new string from a given string without the first and last character if the first or last characters are 'a' otherwise return the original given string

  • برمجة بي اتش بي

Write a PHP program to create a new string from a given string without the first and last character if the first or last characters are 'a' otherwise return the original given string

الأجوبة

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

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

            return $s1;
     }    
 

echo test("abcab") . "\n";
echo test("Python") . "\n";
echo test("abcda") . "\n";
echo test("jython") . "\n";

Sample Output:

bcab
Python
bcd
jython
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...