【发布时间】:2018-10-17 17:43:33
【问题描述】:
- 我正在转换用户输入字符串(成功但...)
- 我想忽略用大括号括起来的字符
- 同时删除最终输出中的大括号
例如,如果我有这个字符串:
$string = "[ABC] This & Text";
function make_post_type($string) {
$needle = array('-', ' ');
$clean = preg_replace("/[^a-zA-Z0-9_\s]/", "", strtolower($string)); // Remove special characters
$haystack = preg_replace('!\s+!', ' ', $clean); // Now remove extra spaces
return str_replace($needle, '_', $haystack);
}
返回abc_this_text
我想回ABC_this_text
【问题讨论】:
-
在同一个字符串上使用 strtolower 两次可能与它有关....
-
@Andreas 那是个错误,代码已更新;)
-
我会将工作分成两部分。一,检测大括号中的字符串,生成新的输出,然后,用[^a-zA-Z[]]+替换成_ ,我不懂很多php,就靠你了。
标签: php regex preg-replace preg-replace-callback