Remove Leading and Trailing Slashes From a String in PHP With Example

Hello Friends Today, through this tutorial, I will tell you How do You Remove Leading and Trailing Slashes From a String in PHP With Example? You can remove leading and trailing slashes from a string in PHP using the `trim()` function. Here’s an example:

<?php
// Sample string with leading and trailing slashes
$string = '/example/string/';

// Removing leading and trailing slashes
$cleanedString = trim($string, '/');

// Display the cleaned string
echo "Original string: $string\n";
echo "Cleaned string: $cleanedString\n";
?>

Output:

Original string: /example/string/
Cleaned string: example/string

In this example, the `trim()` function removes leading and trailing slashes from the `$string` variable. The second argument of `trim()` specifies the characters to be removed, which in this case is the forward slash (`/`).