In this PHP tutorial, you shall learn how to trim specific characters from edges of given string using trim() function, with example programs.

PHP – Trim specific characters from edges of string

To trim specific characters from edges of a string in PHP, we can use trim() function.

Call trim() function and pass the given string and specific characters to remove as arguments. The function returns a new string created by trimming off the specified characters from edges of the original string.

Syntax

The syntax to trim the the specific characters chars around string str using trim()function is

</>
Copy
trim($str, $chars)

where we can specify all the characters that needs to be removed as a string in $chars.

Examples

1. Remove vowels from edges of string

In this example, we take a string str and remove the vowels 'aeiou' from the leading and trialing edges of the string str.

The vowels that are present only on the edges will be removed.

PHP Program

</>
Copy
<?php
  $str = 'appileee';
  $vowels = 'aeiou';
  $output = trim($str, $vowels);
  echo $output;
?>

Output

Conclusion

In this PHP Tutorial, we learned how to delete specific characters from leading and trialing edges of given string, using trim() function.