PHP Doku:: Sucht und ersetzt mit regulären Ausdrücken - function.preg-replace.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzTextverarbeitungReguläre Ausdrücke (Perl-kompatibel)PCRE-Funktionenpreg_replace

Ein Service von Reinhard Neidl - Webprogrammierung.

PCRE-Funktionen

<<preg_replace_callback

preg_split>>

preg_replace

(PHP 4, PHP 5)

preg_replaceSucht und ersetzt mit regulären Ausdrücken

Beschreibung

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

Durchsucht die Zeichenkette subject nach Übereinstimmungen mit pattern und ersetzt sie mit replacement.

Parameter-Liste

pattern

Der Ausdruck, nach dem gesucht wird. Es kann entweder eine Zeichenkette oder ein Array mit Zeichenketten sein.

Mit dem Modifikator e wird der Parameter replacement nach den entsprechenden Ersetzungen der Referenzen von preg_replace() wie PHP-Code behandelt. Tipp: Stellen Sie sicher, dass replacement gültigen PHP-Code erzeugt, weil sich PHP sonst über einen Syntaxfehler (parse error) in der Zeile beschwert, die den Aufruf von preg_replace() enthält.

replacement

Die Zeichenkette oder das Array mit Zeichenketten zum Ersetzen. Falls dieser Parameter eine Zeichenkette ist und der Parameter pattern ein Array, werden alle Suchmuster durch diese Zeichenkette ersetzt. Falls sowohl pattern als auch replacement Arrays sind, wird jedes Suchmuster pattern durch das Gegenstück aus replacement ersetzt. Wenn das replacement-Array weniger Elemente hat als das pattern-Array, wird jedes überzählige pattern durch die leere Zeichenkette ersetzt.

replacement darf Referenzen in der Form \\n oder (ab PHP 4.0.4) $n enthalten, wobei Letztere vorzuziehen ist. Jede dieser Referenzen wird mit dem Text ersetzt, der vom n-ten eingeklammerten Suchmuster erfasst wurde. n kann einen Wert von 0 bis 99 haben. \\0 oder $0 beziehen sich auf den Text, der auf das komplette Suchmuster passt. Um die Nummer des erfassenden Teil-Suchmusters zu erhalten, werden öffnende Klammern mit 1 beginnend von links nach rechts gezählt. Um einen Backslash im Ersatz zu verwenden, muss er verdoppelt werden ("\\\\" PHP-Zeichenkette).

Wenn Sie mit einer Ersetzung arbeiten wollen, in der auf eine Rückreferenzierung direkt eine weitere Zahl folgt (d.h., direkt nach der Übereinstimmmung mit einem Suchmuster soll eine Zahl kommen), können Sie für Ihre Rückreferenzierung nicht die Schreibweise \\1 verwenden. So würde z.B. \\11 die Funktion preg_replace() verwirren, weil sie nicht weiß, ob Sie die Rückreferenzierung \\1 gefolgt von der Zahl 1 wollen oder nur die Rückreferenzierung \\11. In diesem Fall ist die Lösung, \${1}1 zu verwenden. Damit wird eine isolierte Rückreferenzierung $1 erzeugt und die 1 bleibt ein Buchstabensymbol.

Wenn Sie den Modifikator e verwenden, maskiert diese Funktion ein paar Zeichen (nämlich ', ", \ und NULL) in den Zeichenketten, mit denen die Rückreferenzierungen ersetzen werden. Das wird gemacht, um sicherzustellen, dass keine Syntaxfehler entstehen, wenn Rückreferenzierungen verwendet werden, die einfache oder doppelte Anführungszeichen enthalten (z.B. 'strlen(\'$1\')+strlen("$2")'). Vergewissern Sie sich, dass Sie die Zeichenketten-Syntax von PHP kennen, um genau zu wissen, wie die ausgewertete Zeichenkette aussieht.

subject

Die Zeichenkette oder ein Array mit Zeichenketten zum Durchsuchen.

Falls subject ein Array ist, wird das Suchen und Ersetzen auf jedes Element von subject angewandt und der Rückgabewert ist ebenfalls ein Array.

limit

Die maximal mögliche Anzahl von Ersetzungen für jedes Suchmuster in jeder subject. Standardmäßiger Wert: -1 (kein Limit).

count

Falls angegeben, wird dieser Variable die Anzahl vorgenommener Ersetzungen zugewiesen.

Rückgabewerte

preg_replace() gibt ein Array zurück, falls subject ein Array ist, andernfalls eine Zeichenkette.

Falls Übereinstimmungen gefunden wurden, wird die neue Zeichenkette subject zurückgegeben, andernfalls wird subject unverändert zurückgegeben oder NULL, falls ein Fehler auftrat.

Changelog

Version Beschreibung
5.1.0 Den Parameter count hinzugefügt
4.0.4 Die '$n'-Form für den Parameter replacement hinzugefügt
4.0.2 Den Parameter limit hinzugefügt

Beispiele

Beispiel #1 Die Verwendung von Rückreferenzierungen mit darauf folgenden numerischen Literalen

<?php
$zeichenkette 
'15. April 2003';
$suchmuster '/(\d+)\. (\w+) (\d+)/i';
$ersetzung '${2}1,$3';
echo 
preg_replace($suchmuster$ersetzung$zeichenkette);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

April1,2003

Beispiel #2 Die Verwendung von preg_replace() mit indizierten Arrays

<?php
$zeichenkette 
'Der schnelle braune Fuchs sprang über den faulen Hund.';

$suchmuster = array();
$suchmuster[0] = '/schnelle/';
$suchmuster[1] = '/braune/';
$suchmuster[2] = '/Fuchs/';

$ersetzungen = array();
$ersetzungen[2] = 'Bär';
$ersetzungen[1] = 'schwarze';
$ersetzungen[0] = 'langsame';

echo 
preg_replace($suchmuster$ersetzungen$zeichenkette);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Der Bär schwarze langsame sprang über den faulen Hund.

Wenn wir Suchmuster und Ersetzungen mit ksort() sortieren, sollten wir bekommen was wir wollten.

<?php
ksort
($suchmuster);
ksort($ersetzungen);
echo 
preg_replace($suchmuster$ersetzungen$zeichenkette);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Der langsame schwarze Bär sprang über den faulen Hund.

Beispiel #3 Ersetzen mehrerer Werte

<?php
$suchmuster 
= array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/',
                     
'/^\s*{(\w+)}\s*=/');
$ersetzen = array ('\4.\3.\1\2''$\1 =');
echo 
preg_replace($suchmuster$ersetzen'{startDatum} = 1999-5-27');
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

$startDatum = 27.5.1999

Beispiel #4 Die Verwendung des Modifikators 'e'

<?php
preg_replace
("/(<\/?)(\w+)([^>]*>)/e",
             
"'\\1'.strtoupper('\\2').'\\3'",
             
$html_body);
?>

Das wandelt alle HTML-Tags des durchsuchten Textes in Großbuchstaben um.

Beispiel #5 Leerzeichen entfernen

Dieses Beispiel entfernt überschüssige Leerzeichen aus einer Zeichenkette.

<?php
$str 
'foo   o';
$str preg_replace('/\s\s+/'' '$str);
// Das ist jetzt 'foo o'
echo $str;
?>

Beispiel #6 Die Verwendung des Parameters count

<?php
$anzahl 
0;

echo 
preg_replace(array('/\d/''/\s/'), '*''xp 4 to', -$anzahl);
echo 
$anzahl//3
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

xp***to
3

Anmerkungen

Hinweis:

Bei Verwendung von Arrays für pattern und replacement werden die Schlüssel in der Reihenfolge bearbeitet, in der sie im Array vorliegen. Das ist nicht notwendigerweise dieselbe, wie die numerische Reihenfolge der Indizes. Wenn Sie Indizes verwenden, um festzulegen welches pattern durch welchen Ersatz replacement ersetzt werden soll, sollten Sie vor dem Aufruf von preg_replace() ksort() auf jedes Array anwenden.

Siehe auch

  • preg_filter() - Sucht und ersetzt mit regulären Ausdrücken
  • preg_match() - Führt eine Suche mit einem regulären Ausdruck durch
  • preg_replace_callback() - Sucht und ersetzt einen regulären Ausdruck unter Verwendung eines Callbacks
  • preg_split() - Zerlegt eine Zeichenkette anhand eines regulären Ausdrucks
  • preg_last_error() - Liefert den Fehlercode der letzten PCRE RegEx-Auswertung


71 BenutzerBeiträge:
- Beiträge aktualisieren...
easai
21.12.2010 19:40
To extract hiragana, katakana, kanji portion of a Japanese text, explicity set the unicode range AND specify u modifier for the pattern.

<?php

echo "Hiragana -- ";
$pattern ='/[^\x{3040}-\x{309F}]+/u';
$s=preg_replace($pattern,"",$str);
echo
$s."<br />";

echo
"Katakana -- ";
$pattern ='/[^\x{30A0}-\x{30FF}]+/u';
$s=preg_replace($pattern,"",$str);
echo
$s."<br />";

echo
"Kanji -- ";
$pattern ='/[^\x{4E00}-\x{9FBF}]+/u';
$s=preg_replace($pattern,"",$str);
echo
$s."<br />";

?>
Meisam Mulla
10.12.2010 6:38
I needed a function that validated phone numbers provided by a user and if necessary arrange the numbers in this format 555-555-5555. I couldn't find anything so I just wrote one

<?php
function checkPhone($number)
{
    if(
preg_match('^[0-9]{3}+-[0-9]{3}+-[0-9]{4}^', $number)){
        return
$number;
    } else {
       
$items = Array('/\ /', '/\+/', '/\-/', '/\./', '/\,/', '/\(/', '/\)/', '/[a-zA-Z]/');
       
$clean = preg_replace($items, '', $number);
        return
substr($clean, 0, 3).'-'.substr($clean, 3, 3).'-'.substr($clean, 6, 4);
    }
}
?>
ude dot mpco at wotsrabt dot maps-on
8.12.2010 19:30
I find it useful to output HTML form names to the user from time to time while going through the $_GET or $_POST on a user's submission and output keys of the GET or POST array... the only problem being in the name attribute I follow common programming guidelines and have names like the following: eventDate, eventTime, userEmail, etc. Not great to just output to the user-- so I came up with this function. It just adds a space before any uppercase letter in the string.

<?php
function caseSwitchToSpaces( $stringVariableName )
{

$pattern = '/([A-Z])/';
$replacement = ' ${1}';

return
preg_replace( $pattern, $replacement, $stringVariableName );
}

//ex.
echo( caseSwitchToSpaces( "helloWorld" ) );
?>

would output:

"hello World"

You could also do title-style casing to it if desired so the first word isn't lowercase.
contact at tothepointsolution dot com
5.12.2010 19:51
Just something to have in mind...

When using preg_replace, have in mind that variables are calculated. There is no warning issued if the variable is not defined, even when error reporting is set to  E_ALL ^ E_STRICT

As you can see from the below example:

<?php
$text
= 'The price is PRICE ';

$lookFor = 'PRICE';
$replacement = '$100';

echo
$replacement.'<br />';
//will display
//$100

echo preg_replace('/'.$lookFor.'/', $replacement, $text).'<br />';
//Will display
//The price is 0 

echo str_replace($lookFor, $replacement, $text).'<br />';
//Will display
//The price is $100
?>
Terminux (dot) anonymous at gmail
4.12.2010 6:58
This function will strip all the HTML-like content in a string.
I know you can find a lot of similar content on the web, but this one is simple, fast and robust. Don't simply use the built-in functions like strip_tags(), they dont work so good.

Careful however, this is not a correct validation of a string ; you should use additional functions like mysql_real_escape_string and filter_var, as well as custom tests before putting a submission into your database.

<?php

$html
= <<<END
<div id="function.preg-split" class="refentry"> Bonjour1 \t
<div class="refnamediv"> Bonjour2 \t
<h1 class="refname">Bonjour3 \t</h1>
<h1 class=""">Bonjour4 \t</h1>
<h1 class="*%1">Bonjour5 \t</h1>
<body>Bonjour6 \t<//body>>
</ body>Bonjour7 \t<////        body>>
<
a href="image.php" alt="trans" /        >
some leftover text...
     < DIV class=noCompliant style = "text-align:left;" >
... and some other ...
< dIv > < empty>  </ empty>
  <p> This is yet another text <br  >
     that wasn't <b>compliant</b> too... <br   />
     </p>
 <div class="noClass" > this one is better but we don't care anyway </div ><P>
    <input   type= "text"  name ='my "name' value  = "nothin really." readonly>
end of paragraph </p> </Div>   </div>   some trailing text
END;

// This echoes correctly all the text that is not inside HTML tags
$html_reg = '/<+\s*\/*\s*([A-Z][A-Z0-9]*)\b[^>]*\/*\s*>+/i';
echo
htmlentities( preg_replace( $html_reg, '', $html ) );

// This extracts only a small portion of the text
echo htmlentities(strip_tags($html));

?>
arthur at kuhrmeier dot name
30.09.2010 23:38
I needed a function to convert short color notations (e.g. #3a0) to the long version (i.e. #33aa00). As I didn't find one, I immersed into unfamiliar territory: PCRE. But after some trials and errors, I managed to write a function that doubles all hex characters and drops any other.

<?php
function colorlong ($color) {
    return
preg_replace ("/(?(?=[^0-9a-f])[^.]|(.))/i", '$1$1', $color);
}

//--- some examples ---
echo colorlong ('d08');  //--- dd0088 ---
echo colorlong ('bad beer');  //--- bbaaddbbeeee ---
echo colorlong ('nothing');  //--- yep, nothing ;-) ---
echo colorlong ('E6f');  //--- EE66ff: case is not changed ---

?>
tomfmason
26.09.2010 19:27
in response to rysiek

You can do something like:
<?php
function replace_named($string,$args) {
    foreach(
$args as $k=>$v) {
       
$string = preg_replace("/\(\?P\<$k\>(.*)\)/",$v,$string);
    }
    return
$string;
}

$string = 'articles/(?P<year>\d{4})';
$args = array('year'=>2010);
echo
replace_named($string,$args);
?>
rysiek at fwioo dot pl
22.09.2010 2:03
doesn't seem like preg_replace() supports named subpatterns - i.e. '(?P<name>\w+)' - which would be a godsent in a multitude of situations...
AmigoJack
10.07.2010 15:13
If you're using preg_replace() on huge strings you have to be aware of PREG's limitations. In fact, after each preg_xxx() function you should check if PREG internally failed (and by "failure" I don't mean regexp syntax errors).

On default PHP installations you will run into problems when using preg_xxx() functions on strings with a length of more than 100'000 characters. To workaround rare occasions you can use this:

<?php
    $iSet
= 0// Count how many times we increase the limit
   
while( $iSet< 10 ) {  // If the default limit is 100'000 characters the highest new limit will be 250'000 characters
       
$sNewText= preg_replace( $sRegExpPattern, $sRegExpReplacement, $sVeryLongText );  // Try to use PREG

       
if( preg_last_error()== PREG_BACKTRACK_LIMIT_ERROR ) {  // Only check on backtrack limit failure
           
ini_set( 'pcre.backtrack_limit', (int)ini_get( 'pcre.backtrack_limit' )+ 15000 );  // Get current limit and increase
           
$iSet++;  // Do not overkill the server
       
} else {  // No fail
           
$sVeryLongText= $sNewText// On failure $sNewText would be NULL
           
break;  // Exit loop
       
}
    }
?>

However, be careful: 1.) ini_set() may be forbidden on your server; 2.) preg_last_error() doest not exist prior to PHP 5.2.0; 3.) setting a backtrack limit too high may crash PHP (not only the script currently executed). So if you work a lot with long strings you definitly have to look out for a real solution!

See also natedubya's comment (02-Oct-2009 04:08).
matt at mattjanssen dot com
24.06.2010 18:56
To mask credit cards using a single regexp expression (keeping the first and last four digits):

preg_replace('/(?!^.?)[0-9](?!(.){0,3}$)/', '*', '3456-7890-1234-5678')

Keep the FIRST CHARACTER, the LAST FOUR CHARACTER, and any NON-NUMERIC CHARACTERS in-between. Mask (*) everything else.

"3456-7890-1234-5678" = "3***-****-****-5678"
"4567890123456789" = "4***********6789"
"4928-abcd9012-3456" = "4***-abcd****-3456"
"498291842" = "4****1842"

If the regular expression is a bit confusing, (?!) is a "look-ahead not-equals", meaning make sure this does NOT come before or next, but leave it alone.
bvandale at hotmail dot com
4.06.2010 18:47
I needed a regular expression to remove php tags when importing legacy content.  From the comments on this page I arrived at this:

<\?(php)?[\n\s\r]*.*[\n\s\r]*\?>

Worked like a beaut.
mbernard dot webdeveloper at gmail dot com
6.04.2010 17:30
For those who simply want to convert a date from US format (YYYY/MM/DD) to French format (DD/MM/YYYY), here is a useful function :

<?php
function date2fr($date) {
    return
preg_replace(
       
"/([0-9]{4})\/([0-9]{2})\/([0-9]{2})/i",
       
"$3/$2/$1",
       
$date
   
);
}

function
date2us($date) {
    return
preg_replace(
       
"/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/i",
       
"$3/$2/$1",
       
$date
   
);
}
?>
Anonymous
21.03.2010 17:35
Another easy way to extract the width and the height of getimagesize();

<?php

$image
= getimagesize($params['tmp_name']);
$pattern = '/^width=\"(?P<width>\d+)\" height=\"(?P<height>\d+)\"$/';

preg_match($pattern, $image[3], $matches);

?>

This will return the needed values to the $matches array.
(e.g. $matches['width'] and $matches['height'];)

Hope this helps :)

NOTE: I had to change \s with a space in the regular expression because I kept getting the following error:
"Your note contains a bit of text that will result in a line that is too long, even after using wordwrap()."
tom dot h dot anderson at gmail dot com
18.03.2010 23:50
A utf-8 safe way to remove accents:

<?php
/**
 * UTF-8 Normalization of ASCII extended characters
 */
#require_once 'utf8.inc'; # http://hsivonen.iki.fi/php-utf8/
function normalize($string) {
   
$ext = array(192, 193, 194, 195, 196, 197, 224, 225, 226, 227, 228, 229, 199, 231, 200, 201, 202, 203, 232, 233, 234, 235, 204, 205, 206, 207, 236, 237, 238, 239, 210, 211, 212, 213, 214, 216, 242, 243, 244, 245, 246, 248, 209, 241, 217, 218, 219, 220, 249, 250, 251, 252, 221, 255, 253);

   
$norm = array(65, 65, 65, 65, 65, 65, 97, 97, 97, 97, 97, 97, 67, 99, 69, 69, 69, 69, 101, 101, 101, 101, 73, 73, 73, 73, 105, 105, 105, 105, 79, 79, 79, 79, 79, 79, 111, 111, 111, 111, 111, 111, 78, 110, 85, 85, 85, 85, 117, 117, 117, 117, 89, 121, 121);

   
$string = db_UTF8::utf8tounicode($string);
   
// Using array insersect is slower
   
foreach ($ext as $k => $e) {
        if (
$pos = array_search($e, $string)) {
           
$string[$pos] = $norm[$k];
        }
    }
   
$string = db_UTF8::unicodetoutf8($string);
    return
$string;
}
sergei dot garrison at gmail dot com
9.03.2010 22:40
If you want to add simple rich text functionality to HTML input fields, preg_replace can be quite handy.

For example, if you want users to be able to bold text by typing *text* or italicize it by typing _text_, you can use the following function.

<?php
function encode(&$text) {
   
$text = preg_replace('/\*([^\*]+)\*/', '<b>\1</b>', $text);
   
$text = preg_replace('/_([^_]+)_/', '<i>\1</i>', $text);
    return
$text;
    }
?>

This works for nested tags, too, although it will not fix nesting mistakes.

To make this function more efficient, you could put the delimiters (* and _, in this case) and their HTML tag equivalents in an array and loop through them.
hello at weblap dot ro
6.03.2010 13:10
Post slug generator, for creating clean urls from titles.
It works with many languages.

<?php
function remove_accent($str)
{
 
$a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ', 'ŏ', 'Ő', 'ő', 'Œ', 'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ', 'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ', 'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 'ŷ', 'Ÿ', 'Ź', 'ź', 'Ż', 'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ', 'Ǒ', 'ǒ', 'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
 
$b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
  return
str_replace($a, $b, $str);
}

function
post_slug($str)
{
  return
strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'),
  array(
'', '-', ''), remove_accent($str)));
}
?>

Example: post_slug(' -Lo#&@rem  IPSUM //dolor-/sit - amet-/-consectetur! 12 -- ')
will output: lorem-ipsum-dolor-sit-amet-consectetur-12
roscoe-p-coltrane
28.02.2010 7:43
Reading arguments against variables in CSS got me to thinking.

Process your CSS files in a fashion similar to the following. This particular routine is certainly not the most efficient way, but is what I came up with on the spur of the moment. The prefix token is entirely arbitrary - everything between the leading colon and terminating semicolon is the target. In this way, default values can be put in place, and the constant identifiers simply left as comments, should the stylesheet be used without processing; this would also inhibit your editor from emitting errors about your odd syntax. The declaration pattern at the top assumes something like this:

/*@css_const
 [
     bgc_a=#ccccee,
        fc_a=#000099,
     bgc_b=#5555cc,
     bgc_c=#eeeeff,
     bgc_d=#599fee
 ]
*/

...within the target CSS file.

Usage like so:

.Element {
     font-size:10pt;
     color:#000/*fc_a*/;
     background-color:#fff/*bgc_a*/;
}

And then...

<?php
$dec_pat
= '/^\/\*\@css_const\s+\[(.*)\]\s+\*\//Ums';
preg_match_all($dec_pat,$css,$m);
$lhs = array();
$rhs = array();
foreach(
$m[1] as &$p) {
   
$p = explode(",",$p);
    foreach(
$p as &$q) {
        list(
$k,$v) = explode("=",trim($q));
       
$lhs[] = '/(\w+\:).*\/\*' . $k . '\*\/;$/Um';
       
$rhs[] = '\1' . $v . ';';
    }
}
$css = preg_replace($lhs,$rhs,$css);
// spit it out or return it; whatever
?>

...resulting, of course, in:

.Element {
     font-size:10pt;
     color:#000099;
     background-color:#ccccee;
}

Again, efficiency was not the immediate goal, so please don't slay me...
jjorgenson at wilanddirect dot dot dot com
20.02.2010 0:34
I like using canned SQL queries, where specific values and/or field names are separate in some array.  However the %s place-holders of 'printf()' were tedious and error prone because of sequence dependency.  By using 'tag' replacement ( a word having some delimiter(s) around it ) and an associative array, I was able to disconnect the canned query from the data in the array. 

ex:
old style:
<?php
    $sql
= "SELECT %s FROM %s WHERE %s = %s";
   
printf($sql, data["my_field"], data["table_name"], data["my_field"], data["field_value"] );
?>

new style:
<?php
     $sql
= "SELECT <my_field> FROM <table_name> FROM <my_field> = <field_value> ";
    
$outsql = preg_replace("/<(\w\+)>/e","$data[\"$1\"]", $sql );
?>
     NOTE the preg_replace could be optimized to use an annonymous function instead of a "/e" eval of the parm2 expression.

Take this a step further...
Setup your associative array to contain the output from some previous SQL, and the canned SQLs to reference the fields of other queries.  You can then chain your queries where the results of one feeds another.

Hope this helps someone.

-- jj --
Metal Developer
19.01.2010 18:58
This simple pattern will convert images EXIF Datetime (eg. "2009:04:26 05:09:47") to MySQL Datetime (eg. "2009-04-26 05:09:47") only with one line of code.
<?php
$mysql_datetime
= preg_replace('/(\d{2}):(\d{2}):(\d{2})(.*)/', '$1-$2-$3 $4', $exif_datetime);
?>
info AT photosillusions DOT com
21.12.2009 21:03
Bonjour, voici ce qui pourrait aider certain avec cette fonction.
Les / simple ne sont pas détecté.

Hi, There is a simple code I've found to help others.
The simple / are not detected.

mauvais code (code false) :
$contents = preg_replace("/##/MENUG##/","MENU GAUCHE",$contents);

Vous obtenez le message suivant :
You obtain this warning :
 
Warning: preg_replace() [function.preg-replace]: Unknown modifier 'M' in systeme.php on line 56

Vous remplacez les / par des -. Ce qui est passable et fonctionne bien.

You replace / by - . This work good !
Bon code (good code) :
$contents = preg_replace("-##/MENUG##-","MENU GAUCHE",$contents);

M'écrire si vous avez des questions !  :)
Email me for any questions !  :)
natedubya at no spam dot gmail dot com
2.10.2009 18:08
As a pertinent note, there's an issue with this function where parsing any string longer than 94326 characters long will silently return null. So be careful where you use it at.
info at gratisrijden dot nl
2.10.2009 4:48
if you are using the preg_replace with arrays, the replacements will apply as subject for the patterns later in the array. This means replaced values can be replaced again.

Example:
<?php
$text
= 'We want to replace BOLD with the <boldtag> and OLDTAG with the <newtag>';

$patterns = array('/BOLD/i', '/OLDTAG/i');
$replacements = array('<boldtag>', '<newtag>');

echo
preg_replace ($patterns, $replacements, $text);
?>

Output:
We want to replace <b<newtag>> with the <<b<newtag>>tag> and <newtag> with the <newtag>

Look what happend with BOLD.
alammar at gmail dot com
24.07.2009 23:02
I was writing a web crawler and wanted to limit its range to one website. I needed to dynamically escape the urls so I wrote the following function:

<?php

//Escape a string to be used as a regular expression pattern
//Ex: escape_string_for_regex('http://www.example.com/s?q=php.net+docs')
// returns http:\/\/www\.example\.com\/s\?q=php\.net\+docs
function escape_string_for_regex($str)
{
       
//All regex special chars (according to arkani at iol dot pt below):
        // \ ^ . $ | ( ) [ ]
        // * + ? { } ,
       
       
$patterns = array('/\//', '/\^/', '/\./', '/\$/', '/\|/',
 
'/\(/', '/\)/', '/\[/', '/\]/', '/\*/', '/\+/',
'/\?/', '/\{/', '/\}/', '/\,/');
       
$replace = array('\/', '\^', '\.', '\$', '\|', '\(', '\)',
'\[', '\]', '\*', '\+', '\?', '\{', '\}', '\,');
       
        return
preg_replace($patterns,$replace, $str);
}

?>
marco dot demaio at vanylla dot it
24.07.2009 12:56
Maybe it's better to avoid using \w and \W character class matches because they behave differently depending on locale server settings, therefore you will end up with a regular expression match that changes its behavior depending upon server settings.

<?php
  
/*
   according to most websites the regexp sintax [\w] is identical to [A-Za-z0-9_]
  
   See:
   http://en.wikipedia.org/wiki/Regular_expression
   http://msdn.microsoft.com/en-us/library/1400241x(VS.85).aspx  
   */
  
  
$result1 = preg_replace('/[A-Za-z0-9_]*/', '', "test àèìòù test");  
  
$result2 = preg_replace('/\w*/', '', "test àèìòù test");
     
   echo
"<pre>[" . $result1 . "]</pre>"; //ok, it shows: "[ àèìòù ]"
  
echo "<pre>[" . $result2 . "]</pre>"; //WARNING on server other than UK/US locale, it shows: "[  ]"
  
   /*
   Why does this happen?
   preg_replace uses Perl Compatible Regular Expressions and NOT POSIX regular exp sintax.
   Therefore a minor difference is that in the former one the \w charcater class matches also
   the accented characters depending on locale settings.
  
   See: http://perldoc.perl.org/perlre.html#Regular-Expressions
   */
?>
mybodya at gmail dot com
14.07.2009 13:40
This works fine for windows, linux and mac, encoding cp1251

<?php
function valid_filename($str)
    return
preg_replace('/[^0-9a-zа-яіїё\`\~\!\@\#\$\%\^\*\(\)\; \,\.\'\/\_\-]/i', ' ',$str);
}
?>

can be used html without htmlspetialchars()
spcmky at gmail dot com
24.04.2009 16:15
Using this for SEO urls. I had to modify it a bit to get through the word wrap.  Pretty sure you can one line it a lot of it.

<?php
public static function encodeUrlParam ( $string )
{
 
$string = trim($string);
   
  if (
ctype_digit($string) )
  {
    return
$string;
  }
  else
  {     
   
// replace accented chars
   
$accents = '/&([A-Za-z]{1,2})(grave|acute|circ|cedil|uml|lig);/';
   
$string_encoded = htmlentities($string,ENT_NOQUOTES,'UTF-8');

   
$string = preg_replace($accents,'$1',$string_encoded);
     
   
// clean out the rest
   
$replace = array('([\40])','([^a-zA-Z0-9-])','(-{2,})');
   
$with = array('-','','-');
   
$string = preg_replace($replace,$with,$string);
  }

  return
strtolower($string);
}
?>
atifrashid at hotmail dot com
16.04.2009 8:56
To make seo url from title or any text:

<?php
function CleanFileName( $Raw ){
   
$Raw = trim($Raw);
   
$RemoveChars  = array( "([\40])" , "([^a-zA-Z0-9-])", "(-{2,})" );
   
$ReplaceWith = array("-", "", "-");
    return
preg_replace($RemoveChars, $ReplaceWith, $Raw);
}
   
echo
CleanFileName('whatever $4 sd- -sdf- @    sd 8 as +% sdf ;');
?>
hubert at uhoreg dot ca
8.04.2009 2:09
The issue described by arie below is not actually a bug, but expected behaviour.  The PCRE Regular Expression Details says: "The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place. For example, in the "fr" (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w."
arie dot benichou at gmail dot com
9.03.2009 21:50
<?php
//Be carefull with utf-8, even with unicode and utf-8 support enabled, a pretty odd bug occurs depending on your operating system
$str = "Hi, my name is Arié!<br />";
echo
preg_replace('#\bArié\b#u', 'Gontran', $str);
//on windows system, output is "Hi, my name is Gontran<br />"
//on unix system, output is "Hi, my name is Arié<br />"
echo preg_replace('#\bArié(|\b)#u', 'Gontran', $str);
//on windows and unix system, output is "Hi, my name is Gontran<br />"
arkani at iol dot pt
4.03.2009 20:00
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)
akam AT akameng DOT com
18.02.2009 1:02
<?php                   
$converted   
=
array(
//3 of special chars

'/(;)/ie',
'/(#)/ie',
'/(&)/ie',

//MySQL reserved words!
//Check mysql website!
'/(ACTION)/ie', '/(ADD)/ie', '/(ALL)/ie', '/(ALTER)/ie', '/(ANALYZE)/ie', '/(AND)/ie', '/(AS)/ie', '/(ASC)/ie',

//remaining of special chars
'/(<)/ie', '/(>)/ie', '/(\.)/ie', '/(,)/ie', '/(\?)/ie', '/(`)/ie', '/(!)/ie', '/(@)/ie', '/(\$)/ie', '/(%)/ie', '/(\^)/ie', '/(\*)/ie', '/(\()/ie', '/(\))/ie', '/(_)/ie', '/(-)/ie', '/(\+)/ie',
'/(=)/ie', '/(\/)/ie', '/(\|)/ie', '/(\\\)/ie', "/(')/ie", '/(")/ie', '/(:)/'
);

$input_text = preg_replace($converted, "UTF_to_Unicode('\\1')", $text);

function
UTF_to_Unicode($data){

//return $data;
}
?>
The above example useful for filtering input data, then saving into mysql database, it's not need tobe decoded again, just use UTF-8 as charset.
Please Note escaping special chars between delimiter..
Svoop
10.02.2009 14:41
I have written a short introduction and a colorful cheat sheet for Perl Compatible Regular Expressions (PCRE):

http://www.bitcetera.com/en/techblog/2008/04/01/regex-in-a-nutshell/
neamar at neamar dot fr
4.01.2009 21:08
I was needing regular expression with brace matching, but i was not able to find anything for this problem.
So, if i had :
\bold{something \underline{another thing} and another bold thing}
My regexp would stop at the first closing brace, and it seemed to be a common problem with regular expression, often discussed on forums.

So here is the snippet i used, perhaps it'll be useful :
<?php
   
function preg_replace_with_braces($Regexp,$Remplacement,$Texte)
    {
       
preg_match_all($Regexp,$Texte,$Resultats,PREG_SET_ORDER);
       
       
$SVGRemplacement=$Remplacement;
        foreach(
$Resultats as $Resultat)
        {
//For each result
           
$Remplacement=$SVGRemplacement;
            foreach(
$Resultat as $n=>$Match)
            {
//For each set of capturing parenthesis
               
if($n>0 && strpos($Match,'{')!==false)
                {
//We find a open brace in our regexp : we'll need to find the closing one !
                   
$InitialMatch=$Match;
                   
$Offset=strpos($Texte,$Resultat[0]);
                   
$Offset=strpos($Texte,$Match,$Offset);//We move the caret to the good place : let's start !
                   
$Depart=$Offset;
                   
$Taille=strlen($Texte);
                   
$NestingLevel=0;
                    while(
$NestingLevel>=0 && $Offset<$Taille)
                    {
//Browse the string, searching for braces. Perhaps the most important place !
                       
$Offset++;
                        if(
$Texte[$Offset]=='{')
                           
$NestingLevel++;
                        elseif(
$Texte[$Offset]=='}')
                           
$NestingLevel--;
                    }
                   
$Match=substr($Texte,$Depart,$Offset-$Depart);
                   
$Resultat[0]=str_replace($InitialMatch,$Match,$Resultat[0]);
                }
               
$Remplacement=str_replace('$' . $n,$Match,$Remplacement);
            }
           
$Texte=str_replace($Resultat[0],$Remplacement,$Texte);
        }
        return
$Texte;
    }
?>
Hope it'll be useful !
I know it's pretty odd and unclean, but that was a quick workaround i had.
callummann at blueyonder dot co dot uk
1.01.2009 1:31
For BBcode, rather than having two different arrays you can use the same one.

<?php

$bbcode
= array(

"/\[b\](.*?)\[\/b\]/is" => "<strong>$1</strong>",
"/\[u\](.*?)\[\/u\]/is" => "<u>$1</u>",
"/\[url\=(.*?)\](.*?)\[\/b\]/is" => "<a href='$1'>$2</a>"

);
$text = "[b]Text[/b][u]Text[/u]";

$text = preg_replace(array_keys($bbcode), array_values($bbcode), $text);

?>
montana [at] percepticon [dot] com
23.12.2008 21:47
<?php
/*

Coding across linux, mac, and windows gets annoying dealing with errors resulting from EOL format in config files, etc.

Standardize to CRLF format.

Instead of using one regex I just broke this up into smaller groups -
may take longer to execute three statements rather than one,
but it also granularizes the use case.

*/
//different formats all living together
$s = "Testing 1.\r" . "Testing 2.\n" . "Testing 3.\r\n" . "Testing 4.\n\r:END";

$s = preg_replace("/(?<!\\n)\\r+(?!\\n)/", " :REPLACED: \r\n", $s); //replace just CR with CRLF
$s = preg_replace("/(?<!\\r)\\n+(?!\\r)/", " :REPLACED: \r\n", $s); //replace just LF with CRLF
$s = preg_replace("/(?<!\\r)\\n\\r+(?!\\n)/", " :REPLACED: \r\n", $s); //replace misordered LFCR with CRLF

echo $s;

/*

output:

Testing 1. :REPLACED:
Testing 2. :REPLACED:
Testing 3.
Testing 4. :REPLACED:
:END

*/
?>
mdrisser at gmail dot com
28.11.2008 19:13
An alternative to the method suggested by sheri is to remember that the regex modifier '$' only looks at the end of the STRING, the example given is a single string consisting of multiple lines.

Try:
<?php
// Following is 1 string containing 3 lines
$s = "Testing, testing.\r\n"
  
. "Another testing line.\r\n"
  
. "Testing almost done.";

echo
preg_replace('/\.\\r\\n/m', '@\r\n', $s);
?>

This results in the string:
Testing, testing@\r\nAnother testing line@\r\nTesting almost done.
jette at nerdgirl dot dk
19.11.2008 13:47
I use this to prevent users from overdoing repeated text. The following function only allows 3 identical characters at a time and also takes care of repetitions with whitespace added.

This means that 'haaaaaaleluuuujaaaaa' becomes 'haaaleluuujaaa' and 'I am c o o o o o o l' becomes 'I am c o o o l'

<?php
//Example of user input
$str = "aaaaaaaaaaabbccccccccaaaaad d d d   d      d d ddde''''''''''''";

function
stripRepeat($str) {
 
//Do not allow repeated whitespace
 
$str = preg_replace("/(\s){2,}/",'$1',$str);
 
//Result: aaaaaaaaaaabbccccccccaaaaad d d d d d d ddde''''''''''''

  //Do not allow more than 3 identical characters separated by any whitespace
 
$str = preg_replace('{( ?.)\1{4,}}','$1$1$1',$str);
 
//Final result: aaabbcccaaad d d ddde'''

 
return $str;
}
?>

To prevent any repetitions of characters, you only need this:

<?php
$str
= preg_replace('{(.)\1+}','$1',$str);
//Result: abcad d d d d d d de'
?>
7r6ivyeo at mail dot com
17.11.2008 18:25
String to filename:

<?php
function string_to_filename($word) {
   
$tmp = preg_replace('/^\W+|\W+$/', '', $word); // remove all non-alphanumeric chars at begin & end of string
   
$tmp = preg_replace('/\s+/', '_', $tmp); // compress internal whitespace and replace with _
   
return strtolower(preg_replace('/\W-/', '', $tmp)); // remove all non-alphanumeric chars except _ and -
}
?>

Returns a usable & readable filename.
pyromus at gmail dot com
16.10.2008 15:48
You can do seo permalink to your subject with this function as wordpress.

<?php

function seo_permalink($value) {
$turkce=array(
"ş","Ş","ı","(",
")","'","ü","Ü",
"ö","Ö","ç","Ç",
" ","/","*","?",
"ş","Ş","ı","ğ",
"Ğ","İ","ö","Ö",
"Ç","ç","ü","Ü");
$duzgun=array(
"s","S","i","",
"","","u","U",
"o","O","c","C",
"","-","-","",
"s","S","i","g",
"G","I","o","O",
"C","c","u","U");
$value=str_replace($turkce,$duzgun,$value);
$value = preg_replace("@[^A-Za-z0-9\-_]+@i","",$value);
return
$value;
}

echo
"http://www.pyromus.com/";
echo
seo("convert turkish sentence to seo url value");

?>
Sheri
22.09.2008 23:32
The situation described below by dyer85 at gmail on 28-Aug-2008 at 08:41 can be addressed (since PCRE version 7.3) by including "(*ANYCRLF)" at the start of the pattern. Then linebreaks will be detected for line endings typical of unix, mac and PC texts.

When included in the pattern for the example cited, all expected replacements are made.

<?php
$s
= "Testing, testing.\r\n"
  
. "Another testing line.\r\n"
  
. "Testing almost done.";

echo
preg_replace('/(*ANYCRLF)\.$/m', '.@', $s);
?>
tal at ashkenazi dot co dot il
14.09.2008 10:46
after long time of tring get rid of \n\r and <BR> stuff i've came with this...
(i done some changes in clicklein() function...)

<?php
   
function clickable($url){
       
$url                                    =    str_replace("\\r","\r",$url);
       
$url                                    =    str_replace("\\n","\n<BR>",$url);
       
$url                                    =    str_replace("\\n\\r","\n\r",$url);

       
$in=array(
       
'`((?:https?|ftp)://\S+[[:alnum:]]/?)`si',
       
'`((?<!//)(www\.\S+[[:alnum:]]/?))`si'
       
);
       
$out=array(
       
'<a href="$1"  rel=nofollow>$1</a> ',
       
'<a href="http://$1" rel=\'nofollow\'>$1</a>'
       
);
        return
preg_replace($in,$out,$url);
    }

?>
http://www.webb3.net/
7.09.2008 14:16
Hello I am the admin of http://www.webb3.net/ I got this function for templates.

<?php
$file
= "filename.tpl";
$contents = file_get_contents($file);
$website_tpl = 'something';
$text = preg_replace('/\{(\w+)\}/e', '$$1_tpl', $TEMPLATE);
echo
$text
?>

With this if you have a file filename.tpl and you have the text {website} it will replace it with something as you can see above!

-<a href="http://www.webb3.net">http://www.webb3.net/</a>
dyer85 at gmail dot com
29.08.2008 4:41
There seems to be some unexpected behavior when using the /m modifier when the line terminators are win32 or mac format.

If you have a string like below, and try to replace dots, the regex won't replace correctly:

<?php
$s
= "Testing, testing.\r\n"
  
. "Another testing line.\r\n"
  
. "Testing almost done.";

echo
preg_replace('/\.$/m', '.@', $s); // only last . replaced
?>

The /m modifier doesn't seem to work properly when CRLFs or CRs are used. Make sure to convert line endings to LFs (*nix format) in your input string.
tasser at ne8 dot in
23.08.2008 22:42
preg_replace is greedy by default, and the behaviour of ? on the pattern is to make it non-greedy contrary to what i read on PCRE docs.

<?php
$str
="asdfd adsfd aaaadasd";

$str = preg_replace("/(a)(.*)(d)/","a($2)d",$str);
// a(sdfd adsfd aaaadas)d

$str = preg_replace("/(a)(.*)?(d)/U","a($2)d",$str);
// a(s)dfd a()dsfd a(aaa)da(s)d
?>

which is what i wanted.
halityesil [ at at] globya [ dot dot] net
31.07.2008 15:08
<?PHP

function strip_tags_attributes($sSource, $aAllowedTags = FALSE, $aDisabledAttributes = FALSE, $aAllowedProperties = 'font|font-size|font-weight|color' . '|text-align|text-decoration|margin|margin-left' . '|margin-top|margin-bottom|margin-right|padding' . '|padding-top|padding-left|padding-right|padding-bottom' . '|width|height'){

   if( !
is_array( $aDisabledAttributes ) ){
     
$aDisabledAttributes = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavaible', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragdrop', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterupdate', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmoveout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
   }
  
  
$sSource = stripcslashes( $sSource );
           
  
$sSource = strip_tags( $sSource, $aAllowedTags );
       
   if( empty(
$aDisabledAttributes) ){
      return
$sSource;
   }

  
$aDisabledAttributes = @ implode('|', $aDisabledAttributes);
       
  
$sSource = preg_replace('/<(.*?)>/ie', "'<' . preg_replace(array('/javascript:[^\"\']*/i', '/(" . $aDisabledAttributes . ")[ \\t\\n]*=[ \\t\\n]*[\"\'][^\"\']*[\"\']/i', '/\s+/'), array('', '', ' '), stripslashes('\\1')) . '>'", $sSource );
  
$sSource = preg_replace('/\s(' . $aDisabledAttributes . ').*?([\s\>])/', '\\2', $sSource);
           
  
$regexp = '@([^;"]+)?(?<!'. $aAllowedProperties .'):(?!\/\/(.+?)\/)((.*?)[^;"]+)(;)?@is';   
  
$sSource = preg_replace($regexp, '', $sSource);
  
$sSource = preg_replace('@[a-z]*=""@is', '', $sSource);
           
   return
$sSource;
}

?>

Online resource help skype name : globya

good luck !
Anonymous
30.07.2008 18:21
People using functions like scandir with user input and protecting against "../" by using preg_replace make sure you run ir recursivly untill preg_match no-long finds it, because if you don't the following can happen.

If a user gives the path:
"./....//....//....//....//....//....//....//"
then your script detects every "../" and removes them leaving:
"./../../../../../../../"
Which is proberly going back enough times to show root.

I just found this vunrability in an old script of mine, which was written several years ago.

Always do:
<?php
while( preg_match( [expression], $input ) )
{
  
$input = preg_replace( [expression], "", $input );
}
?>
Robert Hartung
30.07.2008 14:34
I just was trying to make a negated replace with a string NOT being in another string.

The code that is actually working:

<?php
$result
= preg_replace('#{(?!disable1|disable2)[a-z0-9]+}#is', '$1', $foo);
?>

This code matches on all strings that do NOT start with "disable1" or "disable2" and contain of a-z0-9. It took me several hours to figuere out this very easy example!

Hope anyone could use it
marcin at pixaltic dot com
26.07.2008 0:56
<?php
   
//:::replace with anything that you can do with searched string:::
    //Marcin Majchrzak
    //pixaltic.com
   
   
$c = "2 4 8";
    echo (
$c); //display:2 4 8

   
$cp = "/(\d)\s(\d)\s(\d)/e"; //pattern
   
$cr = "'\\3*\\2+\\1='.(('\\3')*('\\2')+('\\1'))"; //replece
   
$c = preg_replace($cp, $cr, $c);
    echo (
$c); //display:8*4+2=34
?>
dweintraub at solbright dot com
17.07.2008 0:23
When you use the '$1', '$2', etc. replacement values, they can be either in double or single quotes. There is no need to worry about the dollar sign being interpreted as a variable or not:

<?php
print preg_replace("/I want (\S+) one/", "$1 is the one I want", "I want that one") . "\n";
print
preg_replace("/I want (\S+) one/", '$1 is the one I want', "I want that one") . "\n";
?>

Both lines will print "that is the one I want".
David
11.07.2008 13:59
Take care when you try to strip whitespaces out of an UTF-8 text. Using something like:

<?php
$text
= preg_replace( "{\s+}", ' ', $text );
?>

brokes in my case the letter à which is hex c3a0. But a0 is a whitespace. So use

<?php
$text
= preg_replace( "{[ \t]+}", ' ', $text );
?>

to strip all spaces and tabs, or better, use a multibyte function like mb_ereg_replace.
akniep at rayo dot info
8.07.2008 0:22
preg_replace (and other preg-functions) return null instead of a string when encountering problems you probably did not think about!
-------------------------

It may not be obvious to everybody that the function returns NULL if an error of any kind occurres. An error I happen to stumple about quite often was the back-tracking-limit:
http://de.php.net/manual/de/pcre.configuration.php
#ini.pcre.backtrack-limit

When working with HTML-documents and their parsing it happens that you encounter documents that have a length of over 100.000 characters and that may lead to certain regular-expressions to fail due the back-tracking-limit of above.

A regular-expression that is ungreedy ("U", http://de.php.net/manual/de/reference.pcre.pattern.modifiers.php) often does the job, but still: sometimes you just need a greedy regular expression working on long strings ...

Since, an unhandled return-value of NULL usually creates a consecutive error in the application with unwanted and unforeseen consequences, I found the following solution to be quite helpful and at least save the application from crashing:

<?php

$string_after
= preg_replace( '/some_regexp/', "replacement", $string_before );

// if some error occurred we go on working with the unchanged original string
if (PREG_NO_ERROR !== preg_last_error())
{
   
$string_after = $string_before;
   
   
// put email-sending or a log-message here
} //if

// free memory
unset( $string_before );

?>

You may or should also put a log-message or the sending of an email into the if-condition in order to get informed, once, one of your regular-expressions does not have the effect you desired it to have.
da_pimp2004_966 at hotmail dot com
21.06.2008 8:09
A simple BB like thing..

<?php
function AddBB($var) {
       
$search = array(
               
'/\[b\](.*?)\[\/b\]/is',
               
'/\[i\](.*?)\[\/i\]/is',
               
'/\[u\](.*?)\[\/u\]/is',
               
'/\[img\](.*?)\[\/img\]/is',
               
'/\[url\](.*?)\[\/url\]/is',
               
'/\[url\=(.*?)\](.*?)\[\/url\]/is'
               
);

       
$replace = array(
               
'<strong>$1</strong>',
               
'<em>$1</em>',
               
'<u>$1</u>',
               
'<img src="$1" />',
               
'<a href="$1">$1</a>',
               
'<a href="$1">$2</a>'
               
);

       
$var = preg_replace ($search, $replace, $var);
        return
$var;
}
?>
Michael W
17.04.2008 0:35
For filename tidying I prefer to only ALLOW certain characters rather than converting particular ones that we want to exclude. To this end I use ...

<?php
  $allowed
= "/[^a-z0-9\\040\\.\\-\\_\\\\]/i";
 
preg_replace($allowed,"",$str));
?>

Allows letters a-z, digits, space (\\040), hyphen (\\-), underscore (\\_) and backslash (\\\\), everything else is removed from the string.
Misha
25.03.2008 3:45
This is in response to iasmin at amazingdiscoveries dot org's URL text to link function. Hope this is helpful to someone.

I played with it a bit and came up with this version (there were one or two little errors in the regex I think, also -- it didn't allow various necessary characters).

I start with a URL in brackets (this works for my case):
[http://www.site.com/path/that/may/be_long.php?fun=1]

It returns a link of the URL after the "http://":
www.site.com/path/that/may/b...

-----------
    // Cuts off long URLs at $url_length, and appends "..."
    function reduceurl($url, $url_length) {
        $reduced_url = substr($url, 0, $url_length);
        if (strlen($url) > $url_length) $reduced_url .= '...';
       
        return $reduced_url;
    }

    // Makes URLs with brackets into links
    // The regex searches for "http://" or equivalent, then various character possibilities (I don't know if it might be possible to exploit this if more characters were allowed). The "e" after the regex allows the reduceurl() to be evaluated.

    function url2link($linktext) {
        $linktext = preg_replace("#\[(([a-zA-Z]+://)([a-zA-Z0-9?&%.;:/=+_-]*))\]#e", "'<a href=\"$1\" target=\"_blank\">' . reduceurl(\"$3\", 30) . '</a>'", $linktext);
               
        return $linktext;
    }
php-comments-REMOVE dot ME at dotancohen dot com
29.02.2008 21:02
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!
Jacob Fogg
14.01.2008 22:29
Here is my attempt at cleaning up a file name... it's similar to  what someone else has done however a little cleaner with the addition of the | in the reserved characters... also I clean any characters from x00 to x40 (all non display characters and space) as well as everything greater than 7f and greater (removes the Del character and other non English characters), replacing them with an '_'.

<?php
function clean_filename($filename){//function to clean a filename string so it is a valid filename
 
$reserved = preg_quote('\/:*?"<>|', '/');//characters that are  illegal on any of the 3 major OS's
  //replaces all characters up through space and all past ~ along with the above reserved characters
 
return preg_replace("/([\\x00-\\x20\\x7f-\\xff{$reserved}])/e", "_", $filename);
}
?>

[EDIT BY danbrown AT php DOT net: Inserted typofix/bugfix provided by "Bryan Roach" on 15 January, 2008.]
admin[a-t]saltwaterc[d0t]net
11.12.2007 17:17
<?php
function repl_amp($text)
    {
   
$text=preg_replace("/&(?!amp;)/i", "&", $text);
   
$text=preg_replace("/&#(\d+);/i", "&#$1;", $text); // For numeric entities
   
$text=preg_replace("/&(\w+);/i", "&$1;", $text); // For literal entities
   
return $text;
    }
?>

The RegEx Tester says that the first expression is OK, but when testing with various entities, some of them came out broken. I'd tried to use only 2 preg_replace(); calls instead of three by using the alternative branch from the pattern syntax - which didn't came out well. Sorry for the previous error, and I still hope that someone can find a better alternative.
ulf dot reimers at tesa dot com
7.12.2007 19:28
Hi,

as I wasn't able to find another way to do this, I wrote a function converting any UTF-8 string into a correct NTFS filename (see http://en.wikipedia.org/wiki/Filename).

<?php
function strToNTFSFilename($string)
{
 
$reserved = preg_quote('\/:*?"<>', '/');
  return
preg_replace("/([\\x00-\\x1f{$forbidden}])/e", "_", $string);
}
?>

It converts all control characters and filename characters which are reserved by Windows ('\/:*?"<>') into an underscore.
This way you can safely create an NTFS filename out of any UTF-8 string.
mike dot hayward at mikeyskona dot co dot uk
18.10.2007 17:49
Hi.
Not sure if this will be a great help to anyone out there, but thought i'd post just in case.
I was having an Issue with a project that relied on $_SERVER['REQUEST_URI']. Obviously this wasn't working on IIS.
(i am using mod_rewrite in apache to call up pages from a database and IIS doesn't set REQUEST_URI). So i knocked up this simple little preg_replace to use the query string set by IIS when redirecting to a PHP error page.

<?php
//My little IIS hack :)
if(!isset($_SERVER['REQUEST_URI'])){
 
$_SERVER['REQUEST_URI'] = preg_replace( '/404;([a-zA-Z]+:\/\/)(.*?)\//i', "/" , $_SERVER['QUERY_STRING'] );
}
?>

Hope this helps someone else out there trying to do the same thing :)
sternkinder at gmail dot com
24.08.2007 12:10
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
?>
rob at ubrio dot us
21.08.2007 22:48
Also worth noting is that you can use array_keys()/array_values() with preg_replace like:

<?php
$subs
= array(
 
'/\[b\](.+)\[\/b\]/Ui' => '<strong>$1</strong>',
 
'/_(.+)_/Ui' => '<em>$1</em>'
 
...
  ...
);

$raw_text = '[b]this is bold[/b] and this is _italic!_';

$bb_text = preg_replace(array_keys($subs), array_values($subs), $raw_text);
?>
lehongviet at gmail dot com
25.07.2007 10:15
I got problem echoing text that contains double-quotes into a text field. As it confuses value option. I use this function below to match and replace each pair of them by smart quotes. The last one will be replaced by a hyphen(-).

It works for me.

<?php
function smart_quotes($text) {
 
$pattern = '/"((.)*?)"/i';
 
$text = preg_replace($pattern,"“\\1”",stripslashes($text));
 
$text = str_replace("\"","-",$text);
 
$text = addslashes($text);
  return
$text;
}
?>
131 dot php at cloudyks dot org
17.07.2007 13:37
Based on previous comment, i suggest
( this function already exist in php 6 )

<?php
function unicode_decode($str){
    return
preg_replace(
       
'#\\\u([0-9a-f]{4})#e',
       
"unicode_value('\\1')",
       
$str);
}

function
unicode_value($code) {
   
$value=hexdec($code);
    if(
$value<0x0080)
        return
chr($value);
    elseif(
$value<0x0800)
        return
chr((($value&0x07c0)>>6)|0xc0)
            .
chr(($value&0x3f)|0x80);
    else
        return
chr((($value&0xf000)>>12)|0xe0)
        .
chr((($value&0x0fc0)>>6)|0x80)
        .
chr(($value&0x3f)|0x80);
}
?>

[EDIT BY danbrown AT php DOT net:  This function originally written by mrozenoer AT overstream DOT net.]
mtsoft at mt-soft dot com dot ar
9.07.2007 23:30
This function takes a URL and returns a plain-text version of the page. It uses cURL to retrieve the page and a combination of regular expressions to strip all unwanted whitespace. This function will even strip the text from STYLE and SCRIPT tags, which are ignored by PHP functions such as strip_tags (they strip only the tags, leaving the text in the middle intact).

Regular expressions were split in 2 stages, to avoid deleting single carriage returns (also matched by \s) but still delete all blank lines and multiple linefeeds or spaces, trimming operations took place in 2 stages.

<?php
function webpage2txt($url)
{
$user_agent = “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0);

$ch = curl_init();    // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_PORT, 80);            //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s

curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

$document = curl_exec($ch);

$search = array(@<script[^>]*?>.*?</script>@si’,  // Strip out javascript
‘@<style[^>]*?>.*?</style>@siU’,    // Strip style tags properly
‘@<[\/\!]*?[^<>]*?>@si’,            // Strip out HTML tags
‘@<![\s\S]*?–[ \t\n\r]*>@’,         // Strip multi-line comments including CDATA
‘/\s{2,}/’,

);

$text = preg_replace($search, “\n”, html_entity_decode($document));

$pat[0] = “/^\s+/”;
$pat[2] = “/\s+\$/”;
$rep[0] = “”;
$rep[2] = ” “;

$text = preg_replace($pat, $rep, trim($text));

return $text;
}
?>

Potential uses of this function are extracting keywords from a webpage, counting words and things like that. If you find it useful, drop us a comment and let us know where you used it.
ismith at nojunk dot motorola dot com
21.03.2007 18:47
Be 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.
dani dot church at gmail dot youshouldknowthisone
7.02.2007 20:09
Note that it is in most cases much more efficient to use preg_replace_callback(), with a named function or an anonymous function created with create_function(), instead of the /e modifier.  When preg_replace() is called with the /e modifier, the interpreter must parse the replacement string into PHP code once for every replacement made, while preg_replace_callback() uses a function that only needs to be parsed once.
Alexey Lebedev
7.09.2006 11:21
Wasted several hours because of this:

<?php
$str
='It&#039;s a string with HTML entities';
preg_replace('~&#(\d+);~e', 'code2utf($1)', $str);
?>

This code must convert numeric html entities to utf8. And it does with a little exception. It treats wrong codes starting with &#0

The reason is that code2utf will be called with leading zero, exactly what the pattern matches - code2utf(039).
And it does matter! PHP treats 039 as octal number.
Try <?php print(011); ?>

Solution:
<?php preg_replace('~&#0*(\d+);~e', 'code2utf($1)', $str); ?>
robvdl at gmail dot com
21.04.2006 14:15
For those of you that have ever had the problem where clients paste text from msword into a CMS, where word has placed all those fancy quotes throughout the text, breaking the XHTML validator... I have created a nice regular expression, that replaces ALL high UTF-8 characters with HTML entities, such as ’.

Note that most user examples on php.net I have read, only replace selected characters, such as single and double quotes. This replaces all high characters, including greek characters, arabian characters, smilies, whatever.

It took me ages to get it just downto two regular expressions, but it handles all high level characters properly.

<?php
$text
= preg_replace('/([\xc0-\xdf].)/se', "'&#' . ((ord(substr('$1', 0, 1)) - 192) * 64 + (ord(substr('$1', 1, 1)) - 128)) . ';'", $text);
$text = preg_replace('/([\xe0-\xef]..)/se', "'&#' . ((ord(substr('$1', 0, 1)) - 224) * 4096 + (ord(substr('$1', 1, 1)) - 128) * 64 + (ord(substr('$1', 2, 1)) - 128)) . ';'", $text);
?>
gabe at mudbuginfo dot com
18.10.2004 22:39
It is useful to note that the 'limit' parameter, when used with 'pattern' and 'replace' which are arrays, applies to each individual pattern in the patterns array, and not the entire array.
<?php

$pattern
= array('/one/', '/two/');
$replace = array('uno', 'dos');
$subject = "test one, one two, one two three";

echo
preg_replace($pattern, $replace, $subject, 1);
?>

If limit were applied to the whole array (which it isn't), it would return:
test uno, one two, one two three

However, in reality this will actually return:
test uno, one dos, one two three
steven -a-t- acko dot net
8.02.2004 18:45
People using the /e modifier with preg_replace should be aware of the following weird behaviour. It is not a bug per se, but can cause bugs if you don't know it's there.

The example in the docs for /e suffers from this mistake in fact.

With /e, the replacement string is a PHP expression. So when you use a backreference in the replacement expression, you need to put the backreference inside quotes, or otherwise it would be interpreted as PHP code. Like the example from the manual for preg_replace:

preg_replace("/(<\/?)(\w+)([^>]*>)/e",
             "'\\1'.strtoupper('\\2').'\\3'",
             $html_body);

To make this easier, the data in a backreference with /e is run through addslashes() before being inserted in your replacement expression. So if you have the string

 He said: "You're here"

It would become:

 He said: \"You\'re here\"

...and be inserted into the expression.
However, if you put this inside a set of single quotes, PHP will not strip away all the slashes correctly! Try this:

 print ' He said: \"You\'re here\" ';
 Output: He said: \"You're here\"

This is because the sequence \" inside single quotes is not recognized as anything special, and it is output literally.

Using double-quotes to surround the string/backreference will not help either, because inside double-quotes, the sequence \' is not recognized and also output literally. And in fact, if you have any dollar signs in your data, they would be interpreted as PHP variables. So double-quotes are not an option.

The 'solution' is to manually fix it in your expression. It is easiest to use a separate processing function, and do the replacing there (i.e. use "my_processing_function('\\1')" or something similar as replacement expression, and do the fixing in that function).

If you surrounded your backreference by single-quotes, the double-quotes are corrupt:
$text = str_replace('\"', '"', $text);

People using preg_replace with /e should at least be aware of this.

I'm not sure how it would be best fixed in preg_replace. Because double-quotes are a really bad idea anyway (due to the variable expansion), I would suggest that preg_replace's auto-escaping is modified to suit the placement of backreferences inside single-quotes (which seemed to be the intention from the start, but was incorrectly applied).
thewolf at pixelcarnage dot com
23.10.2003 16:38
I got sick of trying to replace just a word, so I decided I would write my own string replacement code. When that code because far to big and a little faulty I decided to use a simple preg_replace:

<?php
/**
 * Written by Rowan Lewis of PixelCarnage.com
 * $search(string), the string to be searched for
 * $replace(string), the string to replace $search
 * $subject(string), the string to be searched in
 */
function word_replace($search, $replace, $subject) {
    return
preg_replace('/[a-zA-Z]+/e', '\'\0\' == \'' . $search . '\' ? \'' . $replace . '\': \'\0\';', $subject);
}
?>

I hope that this code helpes someone!



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)