忽略PHP中的符号

时间:2016-09-26 05:26:04

标签: php

假设我有这段代码:

<form method="POST">
<input type="text" name="expression">
<input type="submit" name="submitexpression">
</form>

<?php
if(isset($_POST['submitexpression']))
{
$getexpression = $_POST['expression'];

?>

如果我的输入$getexpression值为:((xyz)),如何忽略括号,以便在使用strlen时它只给出3而不是7的值?

3 个答案:

答案 0 :(得分:0)

我认为https://github.com/daao87/ContinuousSpeechRecognizer是更好的解决方案,因为它可以删除除字母之外的任何内容(正如您在评论中所要求的那样):

$numChars = strlen(preg_replace("/[^A-Za-z]/", '', $getexpression));

答案 1 :(得分:0)

您可以使用正则表达式简单地剥离(),如下所示:

<?php
    if( isset($_POST['submitexpression']) ){
        $getexpression = $_POST['expression'];
        // IF YOU MAY NOT KNOW HOW MANY "((" AND "))" TO EXPECT,
        // REGEXP IS A BETTER OPTION, OTHERWISE str_replace WOULD DO:
        $regX          = array("#(^\({1,})#", "#(\){1,}$)#");
        $getexpression = preg_replace($regX, "", $getexpression);

        var_dump($getexpression);
    }

?>

答案 2 :(得分:0)

trim($getexpression , '(())');