XF 2.3 Insert regex expression into DB

Matt C.

Well-known member
Is it possible to insert a regex impression into the database? Trying to import a custom user field with a regex expression attached. I don't get any server errors, but the regex expression will not show up in the custom user field settings in the ACP.

Code:
$this->db()->insertBulk('xf_user_field', [
    [
        'field_id' => 'ah_nintendo',
        'display_group' => 'contact',
        'display_order' => 921,
        'field_type' => 'textbox',
        'field_choices' => '',
        'match_type' => 'regex',
        'match_params' => '^SW-\d{4}-\d{4}-\d{4}$',
        'max_length' => 0,
        'viewable_profile' => 0,
        'viewable_message' => 0,
        'display_template' => '',
        'wrapper_template' => ''
    ]
], 'field_id');

Anyone know of a solution?
 
Firstly, you'll probably want to escape the backslashes. So \\ instead of \ as currently PHP will probably parse it as d rather than \d.

Secondly, the correct format for that field is JSON, so it should look like:

PHP:
'match_params' => '{"regex":"^SW-\\d{4}-\\d{4}-\\d{4}$"}',
 
Firstly, you'll probably want to escape the backslashes. So \\ instead of \ as currently PHP will probably parse it as d rather than \d.

Secondly, the correct format for that field is JSON, so it should look like:

PHP:
'match_params' => '{"regex":"^SW-\\d{4}-\\d{4}-\\d{4}$"}',

Thanks for responding. Doesn't seem to work :(

Screenshot 2025-06-26 at 09-37-35 Edit field Nintendo Friend Code altBoards - Admin control p...webp

match_type reflects, but the regex expression will not appear.
 
Back
Top Bottom