preg match - Calling string in separate file in CodeIgniter -
i'm building profanity/racial slur filter website. have working, preg_match
string quite long. i'm wondering if there way host long string in separate file in codeigniter , call when need in preg_match
.
i have googled , couldn't find anything, thought ask here.
what i'm doing hosting string in model , calling this:
if(preg_match($filterregex)){ databasestuffhere(); }
here few options. depending on how , using string , function, 1 may better others.
config
you store value config, in application/config/config.php
$config['filter_regex'] = 'yourreallylongstring';
the primary config auto-loaded codeigniter, can use so:
$filterregex = $this->config->item('filter_regex'); if(preg_match($filterregex, $subject)) { databasestuffhere(); }
constant
if you're using long string in several places , useful have global access, define as constant in application/config/constants.php
. prevent accidentally redefining value.
define('filter_regex', 'yourreallylongstring');
then use function this:
$filterregex = filter_regex; if(preg_match($filterregex, $subject)) { databasestuffhere(); }
helper
finally, use helper. can load helper when required, or auto-load it. can create own helper in application/helpers/
. this:
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); if ( ! function_exists('filter_slurs')) { function filter_slurs($subject = '') { $filter_regex = 'yourreallylongstring'; if (preg_match($filter_regex, $subject)) { return false; } else { return true; } } }
having function handle may make code easier follow , more meaningful, example, in controller, use this:
$this->load->helper('slur_filter_helper'); //assumes helper file is: slur_filter_helper.php if(filter_slurs($subject)) { //do } else { //do else }
Comments
Post a Comment