Can this PHP code be improved?

Is there a better way to complete this simple task below? As with an array or even with another method?

<?PHP // current way if ($city != NULL) { $city = FilterALLHTML($city); } if ($state != NULL) { $state = FilterALLHTML($state); } if ($title != NULL) { $title = FilterALLHTML($title); } if ($division != NULL) { $division = FilterALLHTML($division); } ?> 

Here is my current function

 function FilterALLHTML($document) { //old array line //"'<[\/\!]*?[^<>]*//?//>'si",// strip html $text = strip_tags($document); $search = array ("/f.?u.?c.?k/i", "/(s|$).?h.?i.?t/i", '/(potspace|mycrib|palbolt)/i'); $text = preg_replace ($search, '', $text); return $text; } 

UPDATE - Alright, my new feature after the suggestions from this post, thanks guys

 function FilterALLHTML($var) { //old array line //"'<[\/\!]*?[^<>]*//?//>'si",// strip html if ($var != null){ $text = strip_tags($var); $search = array ("/f.?u.?c.?k/i", "/(s|$).?h.?i.?t/i", '/(potspace|mycrib|palbolt|pot space)/i'); $text = preg_replace ($search, '', $text); return $text; } return null; } 
+4
source share
8 answers

Modify your FilterALLHTML function to perform a null check and return it null ? Then you can throw away all if s.

Example:

 function FilterALLHTML($input) { if ($input === null) return null; // Original code, I'll just use strip_tags() for a functional example return strip_tags($input); } 

Edit:

I wanted to share an alternative to variable variables, since I don't really like the idea of ​​using string literals instead of variable names. Links in full :)

 function FilterALLHTML(&$text) { if ($text !== null) { // Omitted regex bit for simplicity $text = strip_tags($text); } } $city = "<b>New York</b>"; $state = null; $title = "<i>Mr.</i>"; $fields = array(&$city, &$state, &$title); foreach ($fields as &$var) FilterALLHTML($var); 

(note: the implementation of FilterALLHTML is different from the first example)

+15
source

Yes, use variable variables .

 $vars = array('city','state','title','division'); foreach($vars as $v) { if ($$v != null) $$v = FilterAllHTML($$v); } 

If you know that all variables have been predefined, you do not need a null check. Otherwise, a null check will prevent E_NOTICE errors from starting.

+9
source
 foreach (array('city', 'state', 'title', 'division') as $var) { if ($$var != null) { $$var = FilterALLHTML($$var); } } 

Like Thorarin I would suggest that your FilterALLHTML function check for null instead.

+5
source

Well, you can already consider writing a function because you do the same thing four times.

Assuming FilterALLHTML is not a custom function.

 function Filter($var) { if ($var != null) { return FilterALLHTML($var); } return null; } 

Or simply enable null checking in the FilterALLHTML function and return null if necessary.

So, if you can change FilterALLHTML, you would do it like this:

 function FilterALLHTML($var) { if ($var == null) { return null; } else { //do your filtering return $filteredVar; } } 
+3
source

The answer to zombies is the best, but I would add that you should not really check for null . If for some reason FilterAllHTML has a problem with null values ​​that it should not, put a check for null in the definition of the FilterAllHTML function.

 $vars = array('city', 'state', 'title', 'division'); foreach($vars as $var) { $$var = FilterAllHTML($$var); } 
+3
source

Adding Toranin’s answer, you can change your filterall function to accept an array as input, and passing it by reference, it will change the contents of the arrays.

 $tofilter = array($city,$state,$division,$title); filterall($tofilter); 
+1
source

I have not seen this mentioned, you can always pass parameters by reference to skip re-assignments:

 function FilterALLHTML(&$var) { if ($var == null) { $var = null; } else { $var = strip_tags($var); } } 

I believe that you can also store links in an array, but I have not tried.

 foreach (array(&$city, &$state, &$title, &$division) as $var) { FilterALLHTML($var); } 
+1
source

I don't think you can improve performance, but you can shorten the syntax, but in the end it will be the same for the interpreter

 <?PHP $city = ($city == NULL) ? "default value" : FilterALLHTML($city); $state = ($state == NULL) ? "default value" : FilterALLHTML($state); $title = ($title == NULL) ? "default value" : FilterALLHTML($title); $division = ($division == NULL) ? "default value" : FilterALLHTML($division); ?> 

the "default" should be replaced with what you want the value to be if the variable is null

0
source

All Articles