开发者

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

开发者 https://www.devze.com 2023-01-10 15:46 出处:网络
H开发者_开发知识库ow 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 t

H开发者_开发知识库ow 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?


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-' ,'-');


use trim(field,'-')

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


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: http://us3.php.net/trim

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号