- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: php-best-practices/unnecessary-preg-replace
Language: PHP
Severity: Warning
Category: Best Practices
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.
<?php
$str = "The document is ready for revew.";
$changed = preg_replace("/revew/", "review", $str);
$changed = preg_replace("/\./", "!", $changed);
<?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);
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products