PHP只删除数字

时间:2016-04-16 12:16:35

标签: php str-replace

让我说我有这样一句话document.onload = function(){ var val = document.getElementById("text1").value; var characterCount = 10; if ( val.length > characterCount ) { //Now the character count is greater than 10 document.getElementById("text1").style.backgroundColor = "#f00"; } }; 删除纯号码的最佳方法是什么,以便我只获得"I shoot someone using ak47 and m4s 32 times" 如果有人可以教我如何做到这一点,我将非常高兴。

2 个答案:

答案 0 :(得分:5)

你可以通过正则表达式检查单词边界和数字来做到这一点:

/\b\d+\b/

在这里,您要检查单词边界,后跟任意数量的数字,后跟另一个单词边界。正斜杠是分隔符。

然后您可以使用例如preg_replace将匹配的数字替换为空字符串:

$result = preg_replace('/\b\d+\b/', '', $your_string);

请注意,m4stimes之间最多会有2个空格,但您不会在html输出中看到这个空格。如有必要,您也可以搜索这些内容。

答案 1 :(得分:0)

function remove_numbers($string) {
    $num = array(0,1,2,3,4,5,6,7,8,9);
    return str_replace($num, null, $string);
}