PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php function rtrimNeedle1(string|array $haystack, string $needle): string { return preg_replace("/\\Q$needle\\E$/u", '', $haystack); } function rtrimNeedle2(string|array $haystack, string $needle): string { return preg_replace('#' . preg_quote($needle) . '$#u', '', $haystack); } function rtrimNeedle3(string $haystack, string $needle): string { if (!str_ends_with($haystack, $needle)) { return $haystack; } return substr_replace($haystack, '', -strlen($needle)); } function rtrimNeedle4(string $haystack, string $needle): string { if (str_ends_with($haystack, $needle)) { return substr($haystack, 0, -strlen($needle)); } return $haystack; } var_dump( rtrimNeedle1('this is a hamstrïng', 'strïng'), rtrimNeedle2('this is a hamstrïng', 'strïng'), rtrimNeedle3('this is a hamstrïng', 'strïng'), rtrimNeedle4('this is a hamstrïng', 'strïng'), );
Show:  
Copy Clear