-1

I have a duplicate dynamic number of slash / characters in my variable, for example:

$string = 'test////test2';
$string2 = '///test//test3';
$string3 = 'xsss/////ss/';

i want to remove duplicate / slash character next to each other. so it only leaves one /slash character, like this:

$string = 'test/test2';
$string2 = '/test/test3';
$string3 = 'xsss/ss/';

I already tried the suggestion from this answer

$string = preg_replace('/,+/', '/', $string);
$string = rtrim($string, '/'); 

but it's not working at all, it's still outputting the original string

0

1 Answer 1

0

You almost got it with preg_replace

Just changed it to detect one or more / instead of , (\/+ rule):

$re = '/\/+/';
$str = '///test//test3';

echo preg_replace($re, '/', $str); //outputs /test/test3
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.