I want to store a few regular expressions in a text field in a mysql database. Currently my best idea is to use an irregular pattern of symbols for my delimiter. Example:
$delim = '!@#$%';
$regex1 = '/^(.+)$/';
$regex2 = '/.(.+)./';
$regex3 = '/(\s+):\/\//';
$string = $regex1.$delim.$regex2.$delim.$regex3;
$string would then be stored in the database. To retrieve I’d use something like:
$array = explode($delim,$string);
What I’d like to know is if there’s a better, more accurate method than just hoping my delimiter doesn’t appear in one of the regexs.
Yep, much better way. Use
serialize()orjson_encode()when putting them in the database andunserialize()orjson_decode()when pulling them out.This has the benefit of not failing in a catastrophic way which ends up taking you forever to debug.