Ayesha Ayesha - 2 months ago 7
PHP Question

PHP Trim special characters , - | from left and right automatically

Is there a way to easily trim out specific extra characters from left and right of a php string along with extra spaces and break lines e.g.

$str = " ,apple,orrange,banana, ";


to

apple,orrange,banana


Similarly

$str = "|apple|orrange|banana||";


to

apple|orrange|banana

Answer

PHP trim() function has second parameter Character Mask which can be used in your case.

$fieldValue = ",|banana,orange,Yahooo||| ";
$v = trim($fieldValue," \t\n\r\0\x0B,-|");
echo $v;

The output will be

banana,orange,Yahooo