preg_replace_callback $count number of matches, number of replacements
Nessuno ha ancora preso questa issue.
- Lingua principale
- C
- Stelle
- 40.4k
- Fork
- 8.2k
- Merge medio
- 2g 13h
- PR unite (30g)
- 96
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l’esempio PHP 8.5 collegato e confronta preg_replace con preg_replace_callback. Leggi la documentazione di preg_replace e traccia come il count esistente sia definito come numero di sostituzioni; l’issue è completa quando il progetto ha un comportamento deciso e documentato per i conteggi delle corrispondenze rispetto a quelli delle sostituzioni e la limitazione del callback collegata è stata risolta.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- php
- Ambito
- backend
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100