• Resolved Conal Mullan

    (@conalmullan)


    I am trying to use this code within [insert_php] but it keeps breaking everything, i.e. the code doesnt get interpreted into php and is just rendered as is on the webpage.

    I have had a look around and cant figure out what is causing it to break.
    Any help would be great.

    function isDomain($domain1) {if (preg_match("{^.+\.[a-z]{2,}\$}", $domain1)) { return true; } return false; }

    Thanks.

    https://ww.wp.xz.cn/plugins/insert-php/

Viewing 5 replies - 1 through 5 (of 5 total)
  • ConalMullan, it has to do with the curly braces in preg_match().

    I don’t know exactly what they do in the eval() function (Insert PHP works by putting the code through the eval() function), as I haven’t yet completed my testing.

    In the meantime, this should work as a work-around:

    [insert_php]
    function isDomain($domain1)
    {
       if (preg_match('/^.+\.[a-z][a-z]+$/', $domain1)) { return true; }
       return false;
    }
    echo isDomain('example.com') ? 'YES' : 'no';
    [/insert_php]

    Will

    Sorry, inadvertently ticked the “resolved” box and see no way to un-resolve the thread.

    Thread Starter Conal Mullan

    (@conalmullan)

    Thanks Will – that has worked!

    I have one more that is doing the same thing but apparently for a different reason?

    function isIP($ip1) {return(preg_match('/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'.'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'.'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'.'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/',$ip1));}

    I looks like you’re finding most of them. I apologize.

    Yes, the vertical bar character creates havoc in the eval() function. I’ve never found a work-around for that. It and other elements that don’t work in Insert PHP are talked about here:
    http://www.willmaster.com/software/WPplugins/insert-php-wordpress-plugin-instructions.php#nonworkingelements
    (The curly braces within a regex aren’t listed there, yet. Haven’t finished testing.)

    Perhaps this can be used to validate the characters of an IP address:

    function ValidateIPaddressFormat($ip)
    {
       $arr = explode('.',$ip);
       if( count($arr) != 4 ) { return false; }
       foreach( $arr as $chunk )
       {
          if( preg_match('/\D/',$chunk) ) { return false; }
          if( $chunk > 255 ) { return false; }
       }
       return true;
    }

    Will

    Thread Starter Conal Mullan

    (@conalmullan)

    Thanks Will – all good now.

    I really appreciate your help with those. I was getting a bit stuck!

    Conal.

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

The topic ‘Problem with line of code’ is closed to new replies.