• Resolved wildapache

    (@wildapache)


    Hello.

    Maybe anybody know where I am wrong.

    I have a form with just 1 text filed.

    This form send ajax post request to php script.

    When I send : ABC

    In the server side (PHP) script I can do this

    <?php 

    if(preg_match("/ABC/",$_POST['text'])) {
    echo "FOUND";
    } else {
    echo "NOT FOUND";
    }
    ?>

    OK.

    Now ! If I put in my form text field a symbol like & or some special or accent character the preg_match doesn’t recognize it !

    Before I thinked is won’t woks with a accents char. But after I simple put & simbol and has same result like with a accents char.

    Where I am wrong ?

    For example try to send via form ajax post request with data like :

    hello & world and via preg_match check if it has & symbol..

    I tried different thing but all no has success..

    • This topic was modified 1 week, 2 days ago by wildapache.
    • This topic was modified 1 week, 2 days ago by wildapache.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi @wildapache,

    Have you checked the form output? For example, by running the following code on form submission. It will help to confirm whether PHP is receiving the expected value.

    var_dump($_POST['text']);

    Also, please check how the form data is being sent to your PHP script. Are you using the default application/x-www-form-urlencoded encoding or multipart/form-data?

    Moderator t-p

    (@t-p)

    Are you using a plugin for this form? If, yes, which plugin? Downloaded from?

    Or is it a feature of your current theme? If so which theme? downloaded from?

    Thread Starter wildapache

    (@wildapache)

    Thank for the replies.

    After many attempts, the problem was solved.

    The first problem was is a conflict with another code.

    The second problem with accents chars was – I simply forgot to add u/ in the end of regex

    Because without this important parameter php don’t recognize unicode chars.

    Before I am think it something with a code/encode and headers.. but there was all ok.

    Now I am just adding to regex accent char which I want to pass into my form title and it works.

    I looked to do not replace but put a real accent char to DB and show it after to user. If somebody have similar problem but do not want to add a real accent char to DB exist a simple mode to replace it to a normal chars. Like a’ –> a. You will need just to convert it to htmlentities and after do a simple preg_replace.

    Thread Starter wildapache

    (@wildapache)

    After you fix a problem with accent char you will have suddently another.. is a count a string with an accents chars ! Because strlen(à) is not equal 1

    Hi @wildapache,

    Happy to know you are getting almost the expected results now. Please note that the strlen() function counts bytes, not characters. Refer to this doc for more details: https://www.php.net/manual/en/function.strlen.php#refsect1-function.strlen-returnvalues

    Therefore the following is result is expected.

    strlen('à');   // returns 2 or more, not 1

    Maybe you can try with mb_strlen instead of strlen. Refer to the following doc and sample code for more details.

    https://www.php.net/manual/en/function.mb-strlen.php#refsect1-function.mb-strlen-returnvalues

    https://www.geeksforgeeks.org/php/php-mb_strlen-function/

    $text = $_POST['text'] ?? '';

    //Length check
    $length = mb_strlen($text, 'UTF-8');
    Thread Starter wildapache

    (@wildapache)

    I have build my own function which set all accent char like 1, and after I can use a strlen as normal.

    It’s for do not mix many check function at once.

    In anyway who work with accent char for fist time is not easy for sure.

    Thread Starter wildapache

    (@wildapache)

    I solved all things.

    I can detect any accent chars and convert it in way to check it via strlen like a normal chars.

    After preg_match pass I just cut data in DB with others normal ascii chars.

    Hi @wildapache,

    Happy to know your issue is resolved now. It would be great if you could share the workaround you have tried. It will be helpful for our forum users.

Viewing 8 replies - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.