Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:php-best-practices/unnecessary-preg-replace
Language: PHP
Severity: Warning
Category: Best Practices
Description
The rule emphasizes the use of str_replace over preg_replace when regular expressions are not necessary. This is important because str_replace is faster and consumes less memory than preg_replace, making your PHP code more efficient.
Using regular expressions when they are not necessary can lead to slow performance and increased memory usage. It can also make your code harder to understand since regular expressions can be quite complex.
To avoid violating this rule, always use str_replace when you’re simply replacing one string with another. Only use preg_replace when you need to match a pattern, not a specific string. By following this rule, you can improve the performance of your PHP code and make it easier for other developers to understand.
Non-Compliant Code Examples
<?php$str="The document is ready for revew.";$changed=preg_replace("/revew/","review",$str);$changed=preg_replace("/\./","!",$changed);
Compliant Code Examples
<?php$str="The document is ready for revew.";$changed=str_replace("revew","review",$str);$changed=str_replace(".","!",$changed);$str="The document is ready for revew...";$changed=preg_replace("/\s*revew/"," review",$str);$changed=preg_replace("/\.{3}/","!",$changed);
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- php-best-practices # Rules to enforce PHP best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines