PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

preg_replace_callback> <preg_match
Last updated: Fri, 18 Jul 2008

view this page in

preg_quote

(PHP 4, PHP 5)

preg_quote — Inserisce il carattere di escape nei caratteri delle espressioni regolari

Descrizione

string preg_quote ( string $stringa [, string $delimitatori ] )

La funzione preg_quote() inserisce il carattere di escape (\) davanti ad ogni carattere presente in stringa che sia parte della sintassi di una espressione regolare. Questa funzione è utile nei casi in cui si generino, durante l'esecuzione, delle stringhe da usare come criteri di riconoscimento, e queste possano contenere dei caratteri speciali per le espressioni regolari.

Se viene specificato un carattere come parametro delimitatore , anche a questo sarà anteposto il carattere di escape (\). Ciò è particolarmente utile per porre il carattere di escape nei delimitatori richiesti dalle funzioni PCRE. Il carattere di delimitazione più comunemente utilizzato è /.

I caratteri speciali per le espressioni regolari sono: . \\ + * ? [ ^ ] $ ( ) { } = ! < > | :

Example #1 Esempio di preg_quote()

<?php
$keywords 
"$40 for a g3/400";
$keywords preg_quote($keywords"/");
echo 
$keywords// returns \$40 for a g3\/400
?>

Example #2 Esempio di come rendere in corsivo una qualsiasi parola di un testo

<?php
// In questo esempio, la funzione preg_quote($word) viene usata
// per impedire agli asterischi di avere il loro significato
// speciale per le espressioni regolari.

$testo "Questo libro è *molto* difficile da trovare.";
$parola "*molto*";
$testo preg_replace("/"preg_quote($parola) . "/",
                          
"<i>" $parola "</i>",
                          
$testo);
?>

Nota: Questa funzione è binary-safe (gestisce correttamente i file binari)



add a note add a note User Contributed Notes
preg_quote
bizzigul at hotmail dot fr
30-Jul-2008 06:10
To prevent any problems, try to always use a delimiter that will *almost* not be used inside the regex, such as ` (back quote)

for example: instead of
<?php preg_match('/foo\/bar\//',$somevar); ?>

use

<?php preg_match('`foo/bar/`',$somevar); ?>

it's that simple! like this, you won't have to bother with delimiters anymore...
Anonymous
27-Dec-2007 12:13
Wondering why your preg_replace fails, even if you have used preg_quote?

Try adding the delimiter / - preg_quote($string, '/');

preg_replace_callback> <preg_match
Last updated: Fri, 18 Jul 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites