dongshanyan0322 2015-07-07 16:23
浏览 17
已采纳

如何确保字符串包含更多自动输入的默认值?

I have an input form to create a customField name, when the page loads the textbox is automatically filled with "cst_".

I preset this value in my class:

class customField{

    // DEFINE PROPERTIES
    public $id = 0;
    public $groupId;
    public $name = 'cst_';
    public $displayName;
    public $type;
    public $defaultValue;
    public $required;
    public $orderCount;
    ...

When I go to save the fields on my page, I check to make sure that the user has imputed more values than just leaving the "cst_"

I do this by simply:

// CHECK FOR ONLY "cst_"
if(strlen($customField->name > 4)){

    // SAVE
    $customField->save();

}else{ noticeSet(1,'PLEASE ADD NAME');noticeSet(1,strlen($customField->name));}

in my textbox I added cst_water hardness however I get the error

PLEASE ADD NAME

18

I don't understand how it is failing at that if statement

I've added a noticeSet before the if statement and it shows 18.

Is there any better way to see if a user has added more than just "cst_"?

  • 写回答

3条回答 默认 最新

  • duanmi8349 2015-07-07 16:31
    关注

    You have a parentheses out of place so you end up with ($customField->name > 4) which results in false. So the expression ends up if(strlen(false)) which is obviously no good. Fix it with:

    if(strlen($customField->name) > 4){
    

    If cst_ must be present, then maybe:

    if(strpos($customField->name, 'cst_') === 0 && strlen($customField->name) > 4){
    

    Or shorter but probably slower:

    if(preg_match('/cst_.+/', $customField->name)) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?