PHP Exercises: 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
84. Remove 'a' from First and Last if Present
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.
Sample Solution:
PHP Code :
<?php
// Define a function named 'test' that removes the character 'a' from the beginning and end of a string
function test($s1)
{
// Check if the length of $s1 is greater than 0 and if the last character is 'a'
if (strlen($s1) > 0 && substr($s1, strlen($s1)-1, 1) == "a")
{
// Remove the last character from $s1
$s1 = substr($s1, 0, strlen($s1) - 1);
}
// Check if the length of $s1 is greater than 0 and if the first character is 'a'
if (strlen($s1) > 0 && substr($s1, 0, 1) == "a")
{
// Remove the first character from $s1
$s1 = substr($s1, 1);
}
// Return the modified or original string
return $s1;
}
// Test the 'test' function with different strings, then display the results
echo test("abcab") . "\n";
echo test("Python") . "\n";
echo test("abcda") . "\n";
echo test("jython") . "\n";
?>
Explanation:
- Function Definition:
- The function test is defined with one parameter:
- $s1: the input string to be processed.
- Remove Last Character ('a'):
- The function checks:
- Condition: If the length of $s1 is greater than 0 and if the last character is 'a':
- This is done using substr($s1, strlen($s1)-1, 1).
- Action: If the condition is true, the last character is removed from $s1 using:
- substr($s1, 0, strlen($s1) - 1).
- Remove First Character ('a'):
- The function checks:
- Condition: If the length of $s1 is greater than 0 and if the first character is 'a':
- This is done using substr($s1, 0, 1).
- Action: If the condition is true, the first character is removed from $s1 using:
- substr($s1, 1).
- Return Value:
- The function returns the modified string (with 'a' removed from the beginning or end) or the original string if no modifications were made.
Output:
bcab Python bcd jython
Flowchart:

For more Practice: Solve these Related Problems:
- Write a PHP script to create a new string by removing the first and/or last character if they are the letter 'a'; otherwise, return the original string.
- Write a PHP function to check the beginning and ending of a string for 'a' and strip them if present.
- Write a PHP program to conditionally remove 'a' from the boundaries of a string and output the modified version.
- Write a PHP script to use substring functions to eliminate an 'a' at the start or end of a string and leave it untouched otherwise.
PHP Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a PHP program to create a new string from a given string. If the two characters of the given string from its beginning and end are same return the given string without the first two characters otherwise return the original string.
Next: 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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.