Title: PHP preg match Error
Last modified: August 30, 2016

---

# PHP preg match Error

 *  Resolved [DanSud](https://wordpress.org/support/users/dansud/)
 * (@dansud)
 * [10 years, 12 months ago](https://wordpress.org/support/topic/php-preg-match-error/)
 * I am using the following pattern for validating ClickBank hoplinks.
 * ^http:\/\/[A-Za-z0-9]{5,10}\.[A-Za-z0-9]{5,10}\.hop\.clickbank\.net$
 * But it still gives an error when checking for:
    [http://AFFILIATE.VENDOR.hop.clickbank.net](http://AFFILIATE.VENDOR.hop.clickbank.net)
 * Do I need to modify the pattern? Kindly help.
 * Thanks for creating “ACF: Validated Field”. Its an awesome plugin.
 * [https://wordpress.org/plugins/validated-field-for-acf/](https://wordpress.org/plugins/validated-field-for-acf/)

Viewing 15 replies - 1 through 15 (of 19 total)

1 [2](https://wordpress.org/support/topic/php-preg-match-error/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/php-preg-match-error/page/2/?output_format=md)

 *  Plugin Author [doublesharp](https://wordpress.org/support/users/doublesharp/)
 * (@doublesharp)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204242)
 * Sorry, I’m not super happy with how I implemented the Regex validation. Can’t
   remember the reasoning, but I didn’t want you to have to escape (which is silly)
   and am wrapping the pattern with `/` at the beginning and end, and escaping all
   of them in the middle. This means that you shouldn’t escape the forward slashes,
   but you should escape the dots. Like I said, this isn’t really that great.
 * One alternative is to use the PHP validation and include your pattern there (
   since I’m just doing a PHP regex anyway.
 * If you do want to use the regex, just use this for your pattern:
 *     ```
       ^http://[A-Za-z0-9]{5,10}\.[A-Za-z0-9]{5,10}\.hop\.clickbank\.net$
       ```
   
 * I want to clean this up at some point but haven’t had time to think about how
   the existing field settings would be migrated….
 *  Thread Starter [DanSud](https://wordpress.org/support/users/dansud/)
 * (@dansud)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204292)
 * Hi, thanks for your prompt response. I am not sure the plugin works with the 
   current version of ACF (ver 4.4.2) or WP (Version 4.2.2). Even after removing
   the validation code, it still keeps giving a validation error.
 *  Plugin Author [doublesharp](https://wordpress.org/support/users/doublesharp/)
 * (@doublesharp)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204318)
 * I’ve mostly been testing with ACF5, but it looks like there may have been changes
   for ACF4 as well that require some updates. As soon as I have any more info I’ll
   let you know, in the meantime if you could let me know of anything else specific
   to your issue (javascript errors, messages, php error log, etc) it would be helpful.
 * Thanks!
 *  Plugin Author [doublesharp](https://wordpress.org/support/users/doublesharp/)
 * (@doublesharp)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204396)
 * Hi [@dansud](https://wordpress.org/support/users/dansud/),
 * Can you please try this beta version of the plugin to see if it fixes your issues?
 * [https://downloads.wordpress.org/plugin/validated-field-for-acf.zip](https://downloads.wordpress.org/plugin/validated-field-for-acf.zip)
 * Thanks!
 * Justin
 *  Thread Starter [DanSud](https://wordpress.org/support/users/dansud/)
 * (@dansud)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204397)
 * Hi Justin,
 * I upgraded to the new version. But it still produces an error.
 * Here’s my validation code, to check for a ClickBank hoplink.
 *     ```
       <?php
       $urlalone = substr($value, 7);
   
       $urlparts = explode(".", $urlalone, 3);
   
       $passflag = "N";
   
       if (strlen($urlparts[0]) > 10 or strlen($urlparts[0]) < 5) {
       $passflag = "N";
       }
       else
       {
       $passflag = "Y";
       }
   
       if (strlen($urlparts[1]) > 10 or strlen($urlparts[1]) < 5) {
       $passflag = "N";
       }
       else
       {
       $passflag = "Y";
       }
   
       $pos = strpos($urlparts[2], 'hop.clickbank.net');
   
       if ($pos === false) {
       $passflag = "N";
       }
       else
       {
       $passflag = "Y";
       }
   
       if ($passflag == "Y")
       	return true;
       else
       	return false;
       ?>
       ```
   
 * And here’s how it breaks up the hoplink for testing:
    [http://antarcticacruise.org/blogtesting/xhoplinktest.php](http://antarcticacruise.org/blogtesting/xhoplinktest.php)
 * Thanks for your time.
 * Dan
 *  Plugin Author [doublesharp](https://wordpress.org/support/users/doublesharp/)
 * (@doublesharp)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204398)
 * Hi Dan,
 * There are a few things in your code that would cause the validation to not work
   properly. You are setting the `$passflag` but then not checking it’s value when
   continuing the validation. Since we can only return one string per field, this
   might be a better way of writing the validation. I tested and it verifies that
   the overall format is correct using the regex, then handles the length of the
   AFFILIATE/VENDOR using the matched groups.
 *     ```
       <?php
       $pattern = "~^http://([A-Za-z0-9]+)\.([A-Za-z0-9]+)\.hop\.clickbank\.net$~";
       if ( 0 !== preg_match( $pattern, $value, $matches ) ){
           if ( strlen( $matches[1] ) > 10 ){
               return "The AFFILIATE must be less than 10 characters.";
           } elseif ( strlen( $matches[1] ) < 5 ){
               return "The AFFILIATE must be more than 5 characters.";
           } elseif ( strlen( $matches[2] ) > 10 ){
               return "The VENDOR must be less than 10 characters.";
           } elseif ( strlen( $matches[2] ) < 5 ){
               return "The VENDOR must be more than 5 characters.";
           }
       } else {
           return "Please use a valid URL format.";
       }
       ```
   
 *  Plugin Author [doublesharp](https://wordpress.org/support/users/doublesharp/)
 * (@doublesharp)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204399)
 * I may have spoken too soon, I just found a bug in the ACF5 code. Can you try 
   the validation code above with the latest beta of the plugin?
 * Thanks
 *  Thread Starter [DanSud](https://wordpress.org/support/users/dansud/)
 * (@dansud)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204402)
 * I can’t find ACF5. The links isn’t working for me:
    [https://github.com/AdvancedCustomFields/acf5-beta](https://github.com/AdvancedCustomFields/acf5-beta)
 *  Plugin Author [doublesharp](https://wordpress.org/support/users/doublesharp/)
 * (@doublesharp)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204403)
 * Sorry, that wasn’t clear. I meant there was a bug in my code to support ACF5 (
   the pro version). If you are using 4.4.2 it shouldn’t affect you but I’ve tested
   the validation code above in both versions successfully.
 * Can you give me any more detail about what happens when it fails for you. Screenshots,
   error messages, etc?
 *  Thread Starter [DanSud](https://wordpress.org/support/users/dansud/)
 * (@dansud)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204404)
 * Here’s the screen shot of the error:
    [http://antarcticacruise.org/blogtesting/error.jpg](http://antarcticacruise.org/blogtesting/error.jpg)
 *  Plugin Author [doublesharp](https://wordpress.org/support/users/doublesharp/)
 * (@doublesharp)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204405)
 * That blank error means there is a bad ajax response coming back. In Chrome open
   the developer tools, then click network, then the filter icon followed by “XHR”.
   With the panel open click Publish/Update then select the admin-ajax request in
   the developer panel. Once selected you can view the response, include it in your
   response to me so I can debug. Thanks!
 *  Thread Starter [DanSud](https://wordpress.org/support/users/dansud/)
 * (@dansud)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204406)
 * Hi, this is the error I got:
 *     ```
       <br />
       <b>Warning</b>:  json_encode() expects exactly 1 parameter, 2 given in <b>/home2/danzman/public_html/antarcticacruise.org/blogtesting/wp-content/plugins/validated-field-for-acf/validated_field_v4.php</b> on line <b>450</b><br />
       ```
   
 * Really appreciate your effort. Thanks.
 *  Plugin Author [doublesharp](https://wordpress.org/support/users/doublesharp/)
 * (@doublesharp)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204407)
 * What version of PHP are you using? If you check out the documentation for [`json_encode()`](http://php.net/manual/en/function.json-encode.php)
   it definitely supports more than one parameter…
 * If you can give more more of your system info maybe I can make something work,
   but no promises :/
 *  Plugin Author [doublesharp](https://wordpress.org/support/users/doublesharp/)
 * (@doublesharp)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204408)
 * Ah the options were added in PHP 5.3. I just tagged version 1.7.7 of the plugin
   to check the PHP version and conditionally handle the debug options for `json_encode()`.
 * That said, I would recommend upgrading to a newer version of PHP. It will be 
   a lot faster and use a lot less memory.
 *  Thread Starter [DanSud](https://wordpress.org/support/users/dansud/)
 * (@dansud)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/#post-6204413)
 * My Hostgator server is running PHP Ver 5.4.38

Viewing 15 replies - 1 through 15 (of 19 total)

1 [2](https://wordpress.org/support/topic/php-preg-match-error/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/php-preg-match-error/page/2/?output_format=md)

The topic ‘PHP preg match Error’ is closed to new replies.

 * ![](https://ps.w.org/validated-field-for-acf/assets/icon-256x256.png?rev=1116693)
 * [Advanced Custom Fields: Validated Field](https://wordpress.org/plugins/validated-field-for-acf/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/validated-field-for-acf/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/validated-field-for-acf/)
 * [Active Topics](https://wordpress.org/support/plugin/validated-field-for-acf/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/validated-field-for-acf/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/validated-field-for-acf/reviews/)

## Tags

 * [preg.match](https://wordpress.org/support/topic-tag/preg-match/)

 * 19 replies
 * 2 participants
 * Last reply from: [DanSud](https://wordpress.org/support/users/dansud/)
 * Last activity: [10 years, 11 months ago](https://wordpress.org/support/topic/php-preg-match-error/page/2/#post-6204419)
 * Status: resolved