I am simply trying to compare a value entered into a form, to ensure it begins with a ? mark.

I have tried suggestions I could find online but can't seem to get it to see when the ? is missing

Any suggestions or direction would be greatly appreciated.

    if(!preg_match("/[\s]*(?)/i", $clean_user_aff_code)){
        $user_aff_code_error = "Your Affiliate code MUST begin with a ?";
        $message = 'Please Correct Highlighted Entry Errors';
    }

    if(!preg_match("/^[[:space:]]*?/i", $clean_user_aff_code)){
      $user_aff_code_error = "Your Affiliate code MUST begin with a ?";
      $message = 'Please Correct Highlighted Entry Errors';
    }

Thanks
Douglas

Recommended Answers

All 3 Replies

Regular expressions like this can be more difficult to specify than doing something simple and obvious. IE, iterate over the string character by character and the first non-whitespace character has to be '?'. That is what you want, isn't it?

Member Avatar for diafol

Not sure you need a preg for this. Check the first letter of the string, with:

if(substr($clean_user_aff_code, 0 , 1) !== '?'){
    $user_aff_code_error = "Your Affiliate code MUST begin with a ?";
    $message = 'Please Correct Highlighted Entry Errors';
}
commented: Short and accurate +9

Thank you diafol... not sure where my head was at.. ( well I could guess, but...)

That will do the trick.

Douglas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.