preg_replace_callback $count number of matches, number of replacements
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- C
- Sterne
- 40.4k
- Forks
- 8.2k
- Ø Merge
- 2 T. 13 Std.
- Gemergte PRs (30 T.)
- 96
Beschreibung
Description
The following code:
<?php
$before = 'abca';
$after = preg_replace( '/a/', 'a', $before, -1, $count );
if ( $after === $before ) {
echo "NO REPLACEMENTS" . PHP_EOL;
}
echo $count . " REPLACEMENTS";
Resulted in this output:
NO REPLACEMENTS
2 REPLACEMENTS
But I expected this output instead:
NO REPLACEMENTS
0 REPLACEMENTS
https://www.php.net/manual/en/function.preg-replace.php
If specified, this variable will be filled with the number of replacements done.
Technically, this is correct, since the replacement is identical to the search string.
While for preg_replace this makes practically no difference (except for cases where backslash is incorrectly escaped, you could end up with a preg that says it replaced even though it didn't change the input)
Practically for preg_replace_callback this means however, that you always have to compare before/after to be sure whether anything was replaced at all.
And there's no way of knowing how many replacements were actually done.
Changing the behavior should generally be safe I guess, but otherwise just adding an additional param "$replacement_count" for preg_replace_callback would make sense? (so the 1st param docs would be changed to be "number of matches")
PHP Version
8.5
Operating System
No response
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das verknüpfte PHP-8.5-Beispiel zu reproduzieren, und vergleiche preg_replace mit preg_replace_callback. Lies die Dokumentation zu preg_replace und verfolge, wie der bestehende count als Anzahl der Ersetzungen definiert ist; das Issue ist abgeschlossen, wenn das Projekt ein festgelegtes, dokumentiertes Verhalten für Match- und Ersetzungsanzahlen hat und die verknüpfte Callback-Einschränkung behoben ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- php
- Bereich
- backend
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100