myTIME
myTIME

Reputation: 31

PHP how to remove extra characters at the end and beginning of a string

How can I get rid of extra hyphens at the end and beginning of user submitted text for example. I want visual-studio-2008 instead of -visual-studio-2008- is there a way I can remove the extra hyphens using PHP?

Upvotes: 1

Views: 882

Answers (3)

Siege898
Siege898

Reputation: 463

Use trim and have the second argument be a '-' character. That will remove the hyphens from the beginning and end of the string.

i.e. trim($string, '-')

For more: https://www.php.net/trim

Upvotes: 0

alexn
alexn

Reputation: 59012

Just use trim and supply all characters you want to strip as the second parameter. In your case:

// Yields visual-studio-2008
$string = trim('-visual-studio-2008-' ,'-');

Upvotes: 6

Haim Evgi
Haim Evgi

Reputation: 125614

use trim(field,'-')

http://php.net/manual/en/function.trim.php

Upvotes: 0

Related Questions