1.2.1127. Simplify Regex

Avoid using regex when the searched string or the replacement are simple enough.

PRCE regex are a powerful way to search inside strings, but they also come at the price of performance. When the query is simple enough, try using strpos() or stripos() instead.

<?php

// simple preg calls
if (preg_match('/a/', $string))  {}
if (preg_match('/b/i', $string)) {} // case insensitive

// light replacements
if( strpos('a', $string)) {}
if( stripos('b', $string)) {}       // case insensitive

?>

1.2.1127.1. Suggestions

  • Use str_replace(), strtr() or even strpos()

1.2.1127.2. Specs

Short name

Structures/SimplePreg

Rulesets

All, Performances

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

regex

Examples

Zurmo, OpenConf

Available in

Entreprise Edition, Exakat Cloud