(PHP 4, PHP 5, PHP 7, PHP 8)
preg_replace — Buscar y reemplazar mediante expresión regular estándar
$pattern,$replacement,$subject,$limit = -1,&$count = null
Analiza subject para encontrar la expresión
regular pattern y reemplaza los
resultados por replacement.
Para hacer coincidir una cadena exacta, en lugar de una expresión regular, se recomienda el uso de str_replace() o str_ireplace() en lugar de esta función.
patternEl patrón a buscar. Puede ser una cadena o un array de cadenas.
También están disponibles varios modificadores PCRE.
replacement
La cadena o un array de cadenas para el reemplazo.
Si este parámetro es una cadena y el parámetro pattern
es un array, todos los patrones serán reemplazados por esta cadena.
Si los parámetros pattern y
replacement son arrays, cada
pattern será reemplazado por su
replacement asociado.
Si replacement tiene menos elementos
que pattern, entonces una cadena vacía es
utilizada para los pattern adicionales.
replacement puede contener referencias
de la forma \n o
$n.
Esta última forma es recomendada. Estas referencias serán reemplazadas
por el texto capturado por la n-ésima parentesis
capturante del patrón. n puede tomar valores de
0 a 99, y 0円 o 0ドル, corresponden al texto que
satisface el patrón completo. Los paréntesis abiertos son contados de izquierda a derecha (empezando por 1) para
determinar el número de paréntesis capturante.
Es de notar que en los string literales los backslashs deben
ser escapados.
Cuando se trabaja con un patrón de reemplazo donde una referencia hacia atrás
es seguida directamente por un número (i.e.: colocar un número literal inmediatamente
después de una referencia hacia atrás), no se puede usar la sintaxis clásica
1円 para la referencia hacia atrás. 11円, por ejemplo,
será confuso para la función preg_replace() en el sentido de que
no sabrá si se desea la referencia hacia atrás 1円 seguida del número
1 o si se desea la referencia hacia atrás 11円
seguida de "nada". En este caso, la solución es usar la sintaxis ${1}1.
Esto creará una referencia hacia atrás aislada 1ドル, seguida del número
literal 1.
subjectLa cadena o el array que contiene las cadenas a buscar y reemplazar.
Si subject es un array, entonces la búsqueda y el reemplazo
se realiza en cada entrada de subject, y el array será devuelto.
Si el array subject es asociativo, entonces las claves
serán preservadas en el valor devuelto.
limit
El número máximo de reemplazos para cada patrón en
cada cadena subject. Por omisión, vale
-1 (sin límite).
countSi se proporciona, esta variable contendrá el número de reemplazos realizados.
preg_replace() devuelve un array si el parámetro
subject es un array, o una cadena en caso contrario.
Si se encuentran coincidencias, el nuevo
subject será devuelto, de lo contrario
subject será devuelto sin cambios, o null
si ocurre un error.
Utilizar la opción "\e" es un error; se emite una E_WARNING
en este caso.
Si el patrón regex pasado no se compila a una regex válida, se emite una E_WARNING .
Ejemplo #1 Uso de referencias hacia atrás con literales numéricos
<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,3ドル';
echo preg_replace($pattern, $replacement, $string);
?>El ejemplo anterior mostrará:
April1,2003
Ejemplo #2 Uso de arrays indexados con preg_replace()
<?php
$string = 'Le renard marron agile saute par dessus le chien paresseux.';
$patterns = array();
$patterns[0] = '/agile/';
$patterns[1] = '/marron/';
$patterns[2] = '/renard/';
$replacements = array();
$replacements[2] = 'grizzly';
$replacements[1] = 'brun';
$replacements[0] = 'lent';
echo preg_replace($patterns, $replacements, $string);
?>El ejemplo anterior mostrará:
Le lent brun grizzly saute par dessus le chien paresseux.
Ordenando los patrones y los reemplazos, se debería obtener el resultado esperado.
<?php
$string = 'Le renard marron agile saute par dessus le chien paresseux.';
$patterns = array();
$patterns[0] = '/agile/';
$patterns[1] = '/marron/';
$patterns[2] = '/renard/';
$replacements = array();
$replacements[2] = 'grizzly';
$replacements[1] = 'brun';
$replacements[0] = 'lent';
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);
?>El ejemplo anterior mostrará:
Le grizzly brun lent saute par dessus le chien paresseux.
Ejemplo #3 Reemplazo de múltiples valores simultáneamente
<?php
$patterns = array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/',
'/^\s*{(\w+)}\s*=/');
$replace = array ('3円/4円/1円2円', '$1円 =');
echo preg_replace($patterns, $replace, '{startDate} = 1999年5月27日');
?>El ejemplo anterior mostrará:
$startDate = 5/27/1999
Ejemplo #4 Eliminación de espacios
Este ejemplo elimina los espacios en exceso en una cadena.
<?php
$str = 'foo o';
$str = preg_replace('/\s\s+/', ' ', $str);
// Mostrará 'foo o'
echo $str;
?>
Ejemplo #5 Uso del parámetro count
<?php
$count = 0;
echo preg_replace(array('/\d/', '/\s/'), '*', 'xp 4 to', -1 , $count);
echo "\n";
echo $count; //3
?>El ejemplo anterior mostrará:
xp***to 3
Nota:
Cuando se usan arrays con los parámetros
patternyreplacement, las claves son tratadas en el orden en que aparecen en el array. Esto no es necesariamente lo mismo que el orden de los índices numéricos. Si se usan índices para identificar quépatterndebe ser reemplazado por quéreplacement, se recomienda hacer un ordenamiento ksort() en cada array antes de llamar a preg_replace().
Nota:
Cuando
patternyreplacementson arrays, las reglas de coincidencia funcionarán de manera secuencial. Es decir, la segunda parejapattern/replacementoperará sobre la cadena de caracteres que resulta de la primera parejapattern/replacement, y no sobre la cadena original. Si se desea simular reemplazos funcionando en paralelo, como el intercambio de dos valores, reemplace un patrón por un sustituto intermedio, luego en una pareja posterior, reemplace este marcador intermedio por el reemplazo deseado.<?php $p = array('/a/', '/b/', '/c/'); $r = array('b', 'c', 'd'); print_r(preg_replace($p, $r, 'a')); // imprime d ?>
Because i search a lot 4 this:
The following should be escaped if you are trying to match that character
\ ^ . $ | ( ) [ ]
* + ? { } ,
Special Character Definitions
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Character class
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
More Special Character Stuff
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
033円 octal char (think of a PDP-11)
\x1B hex char
\c[ control char
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase till \E (think vi)
\U uppercase till \E (think vi)
\E end case modification (think vi)
\Q quote (disable) pattern metacharacters till \E
Even More Special Characters
\w Match a "word" character (alphanumeric plus "_")
\W Match a non-word character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a digit character
\D Match a non-digit character
\b Match a word boundary
\B Match a non-(word boundary)
\A Match only at beginning of string
\Z Match only at end of string, or before newline at the end
\z Match only at end of string
\G Match only where previous m//g left off (works only with /g)You can only use numeric backreferences in the replacement string, but not named ones:
<?php
echo preg_replace('#(\d+)#', '1円 1ドル ${1}', '123');
// 123 123 123
echo preg_replace('#(?<digits>\d+)#', '\digits $digits ${digits}', '123');
// \digits $digits ${digits}
?>
To use named backreferences, you have to use preg_replace_callback:
<?php
echo preg_replace_callback('#(?<digits>\d+)#', function( $m ){
return "$m[1]$m[digits]{$m['digits']}";
}, '123');
// 123 123 123
echo preg_replace_callback('#(?<digits>\d+)#', fn($m) => "$m[1]$m[digits]{$m['digits']}", '123');
// 123 123 123
?>
See https://bugs.php.net/bug.php?id=81469 How to replace all comments inside code without remove crln = \r\n or cr \r each line?
<?php
$txt_target=<<<t1
this;// dsdsds
nope
/*
ok
*/
is;huge
/*text bla*/
/*bla*/
t1;
/*
=======================================================================
expected result:
=======================================================================
this;
nope
is;huge
=======================================================================
visualizing in a hex viewer .. to_check_with_a_hex_viewer.txt ...
t h i s ; LF TAB n o p e CR LF CR LF i s ; h u g e CR LF
74 68 69 73 3b 0a 09 6e 6f 70 65 0d 0a 0d 0a 69 73 3b 68 75 67 65 0d 0a
I used F3 (viewer + options 3: hex) in mythical TOTAL COMMANDER!
=======================================================================
*/
echo '<hr><pre>';
echo $txt_target;
echo '</pre>';
// a single line '//' comments
$txt_target = preg_replace('![ \t]*//.*[ \t]*!', '', $txt_target);
// /* comment */
$txt_target = preg_replace('/\/\*([^\/]*)\*\/(\s+)/smi', '', $txt_target);
echo '<hr><pre>';
echo $txt_target;
echo '</pre><hr>';
file_put_contents('to_check_with_a_hex_viewer.txt',$txt_target);
?>To split Pascal/CamelCase into Title Case (for example, converting descriptive class names for use in human-readable frontends), you can use the below function:
<?php
function expandCamelCase($source) {
return preg_replace('/(?<!^)([A-Z][a-z]|(?<=[a-z])[^a-z]|(?<=[A-Z])[0-9_])/', ' 1ドル', $source);
}
?>
Before:
ExpandCamelCaseAPIDescriptorPHP5_3_4Version3_21Beta
After:
Expand Camel Case API Descriptor PHP 5_3_4 Version 3_21 BetaBe aware that when using the "/u" modifier, if your input text contains any bad UTF-8 code sequences, then preg_replace will return an empty string, regardless of whether there were any matches.
This is due to the PCRE library returning an error code if the string contains bad UTF-8.From what I can see, the problem is, that if you go straight and substitute all 'A's wit 'T's you can't tell for sure which 'T's to substitute with 'A's afterwards. This can be for instance solved by simply replacing all 'A's by another character (for instance '_' or whatever you like), then replacing all 'T's by 'A's, and then replacing all '_'s (or whatever character you chose) by 'A's:
<?php
$dna = "AGTCTGCCCTAG";
echo str_replace(array("A","G","C","T","_","-"), array("_","-","G","A","T","C"), $dna); //output will be TCAGACGGGATC
?>
Although I don't know how transliteration in perl works (though I remember that is kind of similar to the UNIX command "tr") I would suggest following function for "switching" single chars:
<?php
function switch_chars($subject,$switch_table,$unused_char="_") {
foreach ( $switch_table as $_1 => $_2 ) {
$subject = str_replace($_1,$unused_char,$subject);
$subject = str_replace($_2,$_1,$subject);
$subject = str_replace($unused_char,$_2,$subject);
}
return $subject;
}
echo switch_chars("AGTCTGCCCTAG", array("A"=>"T","G"=>"C")); //output will be TCAGACGGGATC
?>Below is a function for converting Hebrew final characters to their
normal equivelants should they appear in the middle of a word.
The /b argument does not treat Hebrew letters as part of a word,
so I had to work around that limitation.
<?php
$text="עברית מבולגנת";
function hebrewNotWordEndSwitch ($from, $to, $text) {
$text=
preg_replace('/'.$from.'([א-ת])/u','2ドル'.$to.'1ドル',$text);
return $text;
}
do {
$text_before=$text;
$text=hebrewNotWordEndSwitch("ך","כ",$text);
$text=hebrewNotWordEndSwitch("ם","מ",$text);
$text=hebrewNotWordEndSwitch("ן","נ",$text);
$text=hebrewNotWordEndSwitch("ף","פ",$text);
$text=hebrewNotWordEndSwitch("ץ","צ",$text);
} while ( $text_before!=$text );
print $text; // עברית מסודרת!
?>
The do-while is necessary for multiple instances of letters, such
as "אנני" which would start off as "אןןי". Note that there's still the
problem of acronyms with gershiim but that's not a difficult one
to solve. The code is in use at http://gibberish.co.il which you can
use to translate wrongly-encoded Hebrew, transliterize, and some
other Hebrew-related functions.
To ensure that there will be no regular characters at the end of a
word, just convert all regular characters to their final forms, then
run this function. Enjoy!If you would like to remove a tag along with the text inside it then use the following code.
<?php
preg_replace('/(<tag>.+?)+(<\/tag>)/i', '', $string);
?>
example
<?php $string='<span class="normalprice">55 PKR</span>'; ?>
<?php
$string = preg_replace('/(<span class="normalprice">.+?)+(<\/span>)/i', '', $string);
?>
This will results a null or empty string.
<?php
$string='My String <span class="normalprice">55 PKR</span>';
$string = preg_replace('/(<span class="normalprice">.+?)+(<\/span>)/i', '', $string);
?>
This will results a " My String"A delimiter can be any ASCII non-alphanumeric, non-backslash, non-whitespace character: !"#$%&'*+,./:;=?@^_`|~- and ({[<>]})Sample for replacing bracketed short-codes
The used short-codes are purely used for educational purposes for they could be shorter as in 'italic' to 'i' or 'bold' to 'b'.
Sample text
----
This sample shows how to have [italic]italic[/italic], [bold]bold[/bold] and [underline]underlined[/underline] and [strikethrough]striked[/striketrhough] text.
with this function:
<?php
function textDecoration($html)
{
$patterns = [
'/\[(italic)\].*?\[\/1円\] ?/',
'/\[(bold)\].*?\[\/1円\] ?/',
'/\[(underline)\].*?\[\/1円\] ?/'
];
$replacements = [
'<i>1ドル</i>',
'<strong>1ドル</strong>',
'<u>1ドル</u>'
];
return preg_replace($patterns, $replacements, $html);
}
$html = textDecoration($html);
echo $html; // or return
?>
results in:
----
This sample shows how to have <i>italic</i>, <b>bold</b> and <u>underlined</u> and [strikethrough]striked[/striketrhough] text.
Notice!
There is no [strikethrough]striked[/striketrhough] fallback in the patterns and replacements array