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条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序