如何将两个或多个破折号转换为单打并删除字符串开头和结尾的所有破折号? [英] How can I convert two or more dashes to singles and remove all dashes at the beginning and end of a string?

查看:93
本文介绍了如何将两个或多个破折号转换为单打并删除字符串开头和结尾的所有破折号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:-this--is---a-test--

我想要的是:this-is-a-test

谢谢您的回答! :)

推荐答案

我将结合使用 preg_replace trim :

I would use a combination of preg_replace and trim:

trim(preg_replace('/-+/', '-', $str), '-')

preg_replace调用会删除多个破折号,而trim会删除前导和尾随破折号.

The preg_replace call removes multiple dashes and trim removes the leading and trailing dashes.

这篇关于如何将两个或多个破折号转换为单打并删除字符串开头和结尾的所有破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆