Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

preg_replace

(PHP 3 >= 3.0.9, PHP 4, PHP 5)

preg_replace -- Выполняет поиск и замену по регулярному выражению

Описание

mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )

Выполняет поиск в строке subject совпадений с шаблоном pattern и заменяет их на replacement. В случае, если параметр limit указан, будет произведена замена limit вхождений шаблона; в случае, если limit опущен либо равняется -1, будут заменены все вхождения шаблона.

Replacement может содержать ссылки вида \\n либо (начиная с PHP 4.0.4) $n, причем последний вариант предпочтительней. Каждая такая ссылка, будет заменена на подстроку, соответствующую n'нной заключенной в круглые скобки подмаске. n может принимать значения от 0 до 99, причем ссылка \\0 (либо $0) соответствует вхождению всего шаблона. Подмаски нумеруются слева направо, начиная с единицы.

При использовании замены по шаблону с использованием ссылок на подмаски может возникнуть ситуация, когда непосредственно за маской следует цифра. В таком случае нотация вида \\n приводит к ошибке: ссылка на первую подмаску, за которой следует цифра 1, запишется как \\11, что будет интерпретировано как ссылка на одиннадцатую подмаску. Это недоразумение можно устранить, если воспользоваться конструкцией \${1}1, указывающей на изолированную ссылку на первую подмаску, и следующую за ней цифру 1.

Пример 1. Использование подмасок, за которыми следует цифра

<?php
$string
= "April 15, 2003";
$pattern = "/(\w+) (\d+), (\d+)/i";
$replacement = "\${1}1,\$3";
echo
preg_replace($pattern, $replacement, $string);
?>

Результатом работы этого примера будет:

April1,2003

Если во время выполнения функции были обнаружены совпадения с шаблоном, будет возвращено измененное значение subject, в противном случае будет возвращен исходный текст subject.

Первые три параметра функции preg_replace() могут быть одномерными массивами. В случае, если массив использует ключи, при обработке массива они будут взяты в том порядке, в котором они расположены в массиве. Указание ключей в массиве для pattern и replacement не является обязательным. Если вы все же решили использовать индексы, для сопоставления шаблонов и строк, участвующих в замене, используйте функцию ksort() для каждого из массивов.

Пример 2. Использование массивов с числовыми индексами в качестве аргументов функции preg_replace()

<?php
$string
= "The quick brown fox jumped over the lazy dog.";

$patterns[0] = "/quick/";
$patterns[1] = "/brown/";
$patterns[2] = "/fox/";

$replacements[2] = "bear";
$replacements[1] = "black";
$replacements[0] = "slow";

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

Результат:

The bear black slow jumped over the lazy dog.

Используя ksort(), получаем желаемый результат:

<?php

ksort
($patterns);
ksort($replacements);

echo
preg_replace($patterns, $replacements, $string);

?>

Результат:

The slow black bear jumped over the lazy dog.

В случае, если параметр subject является массивом, поиск и замена по шаблону производятся для каждого из его элементов. Возвращаемый результат также будет массивом.

В случае, если параметры pattern и replacement являются массивами, preg_replace() поочередно извлекает из обоих массивов по паре элементов и использует их для операции поиска и замены. Если массив replacement содержит больше элементов, чем pattern, вместо недостающих элементов для замены будут взяты пустые строки. В случае, если pattern является массивом, а replacement - строкой, по каждому элементу массива pattern будет осущесвтлен поиск и замена на pattern (шаблоном будут поочередно все элементы массива, в то время как строка замены остается фиксированной). Вариант, когда pattern является строкой, а replacement - массивом, не имеет смысла.

Модификатор /e меняет поведение функции preg_replace() таким образом, что параметр replacement после выполнения необходимых подстановок интерпретируется как PHP-код и только после этого используется для замены. Используя данный модификатор, будьте внимательны: параметр replacement должен содержать корректный PHP-код, в противном случае в строке, содержащей вызов функции preg_replace(), возникнет ошибка синтаксиса.

Пример 3. Замена по нескольким шаблонам

<?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");
?>

Этот пример выведет:

$startDate = 5/27/1999

Пример 4. Использование модификатора /e

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

Преобразует все HTML-теги к верхнему регистру

Пример 5. Конвертор HTML в текст

<?php
// $document на выходе должен содержать HTML-документ.
// Необходимо удалить все HTML-теги, секции javascript,
// пробельные символы. Также необходимо заменить некоторые
// HTML-сущности на их эквивалент.

$search = array ("'<script[^>]*?>.*?</script>'si"// Вырезает javaScript
                
"'<[\/\!]*?[^<>]*?>'si",           // Вырезает HTML-теги
                
"'([\r\n])[\s]+'",                 // Вырезает пробельные символы
                
"'&(quot|#34);'i",                 // Заменяет HTML-сущности
                
"'&(amp|#38);'i",
                
"'&(lt|#60);'i",
                
"'&(gt|#62);'i",
                
"'&(nbsp|#160);'i",
                
"'&(iexcl|#161);'i",
                
"'&(cent|#162);'i",
                
"'&(pound|#163);'i",
                
"'&(copy|#169);'i",
                
"'&#(\d+);'e");                    // интерпретировать как php-код

$replace = array ("",
                 
"",
                 
"\\1",
                 
"\"",
                 
"&",
                 
"<",
                 
">",
                 
" ",
                 
chr(161),
                 
chr(162),
                 
chr(163),
                 
chr(169),
                 
"chr(\\1)");

$text = preg_replace($search, $replace, $document);
?>

Замечание: Параметр limit доступен в PHP 4.0.1pl2 и выше.

Смотрите также preg_match(), preg_match_all(), и preg_split().



preg_split> <preg_replace_callback
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
preg_replace
info at oscaralexander dot com
28-Oct-2007 03:38
@ Mac: Your adjustment only seems to work when the period or comma is followed by a newline. The code breaks when more characters follow.

Try the following code instead. It parses the url to a point where all punctuation is followed by either a word character or a slash.

<?php

$string
= preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is', '\\1<a href="\\2">\\2</a>', $string);
$string = preg_replace('#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is', '\\1<a href="http://\\2">\\2</a>', $string);
$string = preg_replace('#(^|\s)(([a-z0-9._%+-]+)@(([.-]?[a-z0-9])*))#is', '\\1<a href="mailto:\\2">\\2</a>', $string);

?>

This seems fairly waterproof but please post your improvements.
Santosh Patnaik
23-Oct-2007 11:35
@giel dot berkers

Use the 'PCRE_DOTALL' ('s') option so that the '.' covers newline characters:

$code = preg_replace('/\/\*.*\*\//ms', '', $code);
mike dot hayward at mikeyskona dot co dot uk
18-Oct-2007 08: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.

<?
//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 :)

If anyone finds a better way, please let met know, I'm still learning ;)
anonymous at example dot com
10-Oct-2007 03:38
To djurredenboer:

It's because PHP stores array elements in order of their creation.

Use sort() or ksort() functions to change the order of your arrays!
djurredenboer at hotmail dot com
10-Oct-2007 12:10
I found something strange
<?php
$string
= 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo
preg_replace($patterns, $replacements, $string);
?>
Output The bear black slow jumped over the lazy dog.

But when you swap the replacements like
$replacements[0] = 'slow';
$replacements[2] = 'bear';
$replacements[1] = 'black';
you get The slow bear black jumped over the lazy dog.
Mac
08-Oct-2007 07:18
Here's a derivate of Iasmin's function.
This one handles URL's followed by a period or comma too and adds a _blank target to the hyperlinks.
Additionally it replaces e-mail addresses with a mailto hyperlink.

<?php
function hyperlink($text) {
   
// match protocol://address/path/file.extension?some=variable&another=asf%
   
$text = preg_replace("/\s([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*
            [a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/i"
,
           
" <a href=\"$1\" target=\"_blank\">$1</a>$2", $text);
   
// match www.something.domain/path/file.extension?some=variable&another=asf%
   
$text = preg_replace("/\s(www\.[a-z][a-z0-9\_\.\-]*
            [a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/i"
,
           
" <a href=\"http://$1\" target=\"_blank\">$1</a>$2", $text);
   
// match name@address
   
$text = preg_replace("/\s([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*
            \@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})([\s|\.|\,])/i"
,
           
" <a href=\"mailto://$1\">$1</a>$2", $text);
    return
$text;
}
?>
matt
28-Sep-2007 08:52
at below post:
<?php
 $template
= "Price: #price#";
 
$price    = '&#36;5';
 print
"Price: $price\n";
 
$res    = preg_replace("/#price#/", $price, $template);
 print
"From template: -> $res\n";

 
?>
jefkin at gmail dot com
20-Sep-2007 06:52
@ Santosh Patnaik

The perl regular expression engine will handle this expression better and much faster by using the word boundry escape code \b.  Though it may not be obvious except to long time perl geeks such as I :)

so:

// Expect and get 'pa pa pa pa'
echo preg_replace('`\bma\b`', 'pa', 'ma ma ma ma');

Jeff
Santosh Patnaik
13-Sep-2007 11:11
Once a match is identified, the regular expression engine appears to set aside the matching segment of the target string. A second segment that you expect to match may therefore end up not getting matched:

// Expect 'pa pa pa pa' but get 'pa ma pa ma'
echo preg_replace('`(^|\s)ma(\s|$)`', '$1pa$2', 'ma ma ma ma');

Here the issue can be solved by using a 'lookahead':

// Expect and get 'pa pa pa pa'
echo preg_replace('`(^|\s)ma(?=\s|$)`', '$1pa', 'ma ma ma ma');
iasmin at amazingdiscoveries dot org
12-Sep-2007 07:37
I thought that someone could use this hyperlink function.
preg_replace is about 6 times faster than ereg_replace. I took the original example from the ereg_replace function page and modified so that it works perfect. I gave a comment of what it matches.
One thing is that I added a space at the beginning so that only links that don't have <a href="" around them or anything else touching will be replaced.

<i>NOTE! I had to break the long lines otherwise I couldn't have posted this. So take the new line out and it will work</i>
<?php
function hyperlink(&$text)
    {
      
// match protocol://address/path/file.extension?some=variable&another=asf%
      
$text = preg_replace("/\s(([a-zA-Z]+:\/\/)([a-z][a-z0-9_\..-]*
[a-z]{2,6})([a-zA-Z0-9\/*-?&%]*))\s/i"
, " <a href=\"$1\">$3</a> ", $text);
   
      
// match www.something.domain/path/file.extension?some=variable&another=asf%
      
$text = preg_replace("/\s(www\.([a-z][a-z0-9_\..-]*
[a-z]{2,6})([a-zA-Z0-9\/*-?&%]*))\s/i"
, " <a href=\"http://$1\">$2</a> ", $text);
      
       return
$text;
    }
?>

Play around with it and see how it works.
Courtesy of AmazingDiscoveries.org
God bless, Iasmin Balaj
robert at codehelpers dot com
01-Sep-2007 05:40
If you're wanting to strip a variety of timestamps, try this:

<?php

$var
= "[09:21:32] Testing";
$var = preg_replace('/\W*\d+:\d+(?::\d+)?[a-zA-Z]*[\W\s]*/', '', $var);
echo
$var;

?>

Hope this helps :)
as
27-Aug-2007 06:36
What about strtr:

<?php
$DNA
= "AGTCTGCCCTAG";

echo
"$DNA\n";

$DNA = strtr($DNA,"AGCT","TCGA");

echo
"$DNA\n";
?>

Andreas
sternkinder at gmail dot com
24-Aug-2007 03: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:

$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:

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-Aug-2007 01:48
Also worth noting is that you can use array_keys()/array_values() with preg_replace like:

$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);
pcabc at chello dot hu
13-Aug-2007 05:50
if (!function_exists('htmlspecialchars_decode')) {
    function htmlspecialchars_decode($str)
    {
        $str = preg_replace('/\\\"/', '"', $str);
        return strtr($str, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
    }
}

a little correcting, change back the \" string to "...
flar at forgetably dot com
30-Jul-2007 05:32
For completeness, with regard to my previous message, it should be noted that the following expressions are functionally identical in PHP:

'/big(.*)house/U'
'/big(.*?)house/'

Last note: The parenthesis can be omitted.
flar at forgetably dot com
28-Jul-2007 05:07
Special care needs to be taken, since all regular expressions are "greedy" by default. What does it mean? Example:

Text: 'the big scary house here and the big weird house over there'
Expression '/big(.*)house/'
Matches: 'big scary house here and the big weird house'

In other words, the expression matches everything between the first occurrence of the word "big" and the last occurrence of the word "house".

If you need to preserve the words 'here and the', you need a non-greedy expression.

If you need a non-greedy regular expression in PHP, add the character "U" after the final slash. So our updated example will look like this:

Expression: '/big(.*)house/U'
Match 1: 'big scary house'
Match 2: 'big weird house'

Note that with the non-greedy expression we now got two matches and the words between the matches are preserved (not replaced).
lehongviet at gmail dot com
25-Jul-2007 01: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.

function smart_quotes($text) {
 $pattern = '/"((.)*?)"/i';
 $text = preg_replace($pattern,"&#147;\\1&#148;",stripslashes($text));
$text = str_replace("\"","-",$text);
$text = addslashes($text);
return $text;
}
alexandre at NO-DAMN-SPAM-gaigalas dot net
24-Jul-2007 01:46
Match and replace for arrays. Useful for parsing entire $_POST

<?php

function array_preg_match(array $patterns, array $subjects, &$errors = array()) {
   
$errors = array();
    foreach (
$patterns as $k => $v) preg_match($v, $subjects[$k]) or $errors[$k] = TRUE;   
    return
count($errors) == 0 ? TRUE : FALSE;
}

function
array_preg_replace(array $patterns, array $replacements, array $subject) {
   
$r = array();
    foreach (
$patterns as $k => $v) $r[$k] = preg_replace($v, $replacements[$k], $subject[$k]);   
    return
$r+$subject;
}

?>
131 dot php at cloudyks dot org
17-Jul-2007 04:37
Based on previous comment, i suggest
( this function already exist in php 6 )

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);
}
anzenews at volja dot net
13-Jul-2007 05:38
As steven -a-t- acko dot net explained, using /e modifier is tricky if strings have quotes in them. However, the solution might be easier than manual coding of some sort of stripslashes - just use preg_replace_callback instead of preg_replace. It is safer anyway.
anon
12-Jun-2007 05:29
For the benefit of perl coders,

$s =~ s/PATTERN/REPLACEMENT/g;

becomes:

<?
$s = preg_replace('/PATTERN/', 'REPLACEMENT', $s);
?>

Note that you have to assign the result back to $s.  If your preg_replace doesn't seem to be working, you may have merely forgotten to assign the return to $s.
igasparetto at hotmail dot com
26-Apr-2007 01:21
Displaying results of a search engine:

$words=explode(" ", $_POST['query']);
foreach($words as $word){
    $patterns[]='/'.$word.'/i';
    $replaces[]='<span class="textFound">$0</span>';
}

// run sql

$display_results="";

foreach($res as $row)
    $display_results .= "<p>" . preg_replace($patterns, $replaces, nl2br(htmlentities( $row['field'] ) ) ) . "</p>\n";

echo $display_results;
ismith at nojunk dot motorola dot com
21-Mar-2007 10: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.
mrozenoer at overstream dot net
06-Mar-2007 08:30
I could not find a function to unescape javascript unicode escapes anywhere (e.g., "\u003c"=>"<").

<?php
function js_uni_decode($s) {
    return
preg_replace('/\\\u([0-9a-f]{4})/ie', "chr(hexdec('\\1'))"$s);
}
echo
js_uni_decode("\u003c");
?>
dani dot church at gmail dot youshouldknowthisone
07-Feb-2007 11: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.
JON
31-Jan-2007 03:41
for a url explode I would suggest parse_url($url). Its far simpler than the list of preg_replaces used.
Alexey Lebedev
07-Sep-2006 02:21
Wasted several hours because of this:

$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 print(011);

Solution:
preg_replace('~&#0*(\d+);~e', 'code2utf($1)', $str);
tim at t-network dot nl
18-Jul-2006 10:58
This function has a little quirk.

When you are trying to use backreferences in the pattern, you MUST use \\n, and not $n. $n doesn't work.
robvdl at gmail dot com
21-Apr-2006 05: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 &#8217;.

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.

$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);
Eric
09-Apr-2006 11:54
Here recently I needed a way to replace links (<a href="blah.com/blah.php">Blah</a>) with their anchor text, in this case Blah. It might seem simple enough for some..or most, but at the benefit of helping others:

<?php

$value
= '<a href="http://www.domain.com/123.html">123</a>';

echo
preg_replace('/<a href="(.*?)">(.*?)<\\/a>/i', '$2', $value);

//Output
// 123

?>
ae at instinctive dot de
28-Mar-2006 07:40
Something innovative for a change ;-) For a news system, I have a special format for links:

"Go to the [Blender3D Homepage|http://www.blender3d.org] for more Details"

To get this into a link, use:

$new = preg_replace('/\[(.*?)\|(.*?)\]/', '<a href="$2" target="_blank">$1</a>', $new);
SG_01
19-Jan-2006 04:43
Re: wcc at techmonkeys dot org

You could put this in 1 replace for faster execution as well:

<?php

/*
 * Removes all blank lines from a string.
 */
function removeEmptyLines($string)
{
   return
preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);
}

?>
kyle at vivahate dot com
22-Dec-2005 12:08
Here is a regular expression to "slashdotify" html links.  This has worked well for me, but if anyone spots errors, feel free to make corrections.

<?php
$url
= '<a attr="garbage" href="http://us3.php.net/preg_replace">preg_replace - php.net</a>';
$url = preg_replace( '/<.*href="?(.*:\/\/)?([^ \/]*)([^ >"]*)"?[^>]*>(.*)(<\/a>)/', '<a href="$1$2$3">$4</a> [$2]', $url );
?>

Will output:

<a href="http://us3.php.net/preg_replace">preg_replace - php.net</a> [us3.php.net]
istvan dot csiszar at weblab dot hu
21-Dec-2005 01:53
This is an addition to the previously sent removeEvilTags function. If you don't want to remove the style tag entirely, just certain style attributes within that, then you might find this piece of code useful:

<?php

function removeEvilStyles($tagSource)
{
  
// this will leave everything else, but:
   
$evilStyles = array('font', 'font-family', 'font-face', 'font-size', 'font-size-adjust', 'font-stretch', 'font-variant');

   
$find = array();
   
$replace = array();
   
    foreach (
$evilStyles as $v)
    {
       
$find[]    = "/$v:.*?;/";
       
$replace[] = '';
    }
   
    return
preg_replace($find, $replace, $tagSource);
}

function
removeEvilTags($source)
{
   
$allowedTags = '<h1><h2><h3><h4><h5><a><img><label>'.
       
'<p><br><span><sup><sub><ul><li><ol>'.
       
'<table><tr><td><th><tbody><div><hr><em><b><i>';
   
$source = strip_tags(stripslashes($source), $allowedTags);
    return
trim(preg_replace('/<(.*?)>/ie', "'<'.removeEvilStyles('\\1').'>'", $source));
}

?>
jhm at cotren dot net
18-Feb-2005 02:04
It took me a while to figure this one out, but here is a nice way to use preg_replace to convert a hex encoded string back to clear text

<?php
    $text
= "PHP rocks!";
   
$encoded = preg_replace(
          
"'(.)'e"
         
,"dechex(ord('\\1'))"
         
,$text
   
);
    print
"ENCODED: $encoded\n";
?>
ENCODED: 50485020726f636b7321
<?php
   
print "DECODED: ".preg_replace(
      
"'([\S,\d]{2})'e"
     
,"chr(hexdec('\\1'))"
     
,$encoded)."\n";
?>
DECODED: PHP rocks!
gabe at mudbuginfo dot com
18-Oct-2004 01: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
08-Feb-2004 09: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).

preg_split> <preg_replace_callback
Last updated: Sat, 27 Jan 2007
 
 
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571