首页 > 解决方案 > 我如何删除在php中两边都有空格的连字符

问题描述

这是我要删除的带有连字符的字符串,但两边都有空格

Year 7 Scheme of Learning – Lord of the Flies/Transactional Writing HAO

标签: php

解决方案


我会使用 str_replace https://www.php.net/manual/en/function.str-replace.php

$string = "Year 7 Scheme of Learning – Lord of the Flies/Transactional Writing HAO";

// remove spaces and hyphen
$newString = str_replace(" - ","",$string);

// remove 1 space and hyphen
$newString = str_replace(" -","",$string);

// remove hyphen
$newString = str_replace("-","",$string);

推荐阅读