<?
/*
��������� ��� ��������. ���� ������������� �������� � win-���������, �� ���
"����������":) - �� ����� ����, ������ ����������������� (win � koi).
*/
define("TAG1","\xAC");
define("TAG2","\xAD");
define("LAQUO","\xAB");
define("RAQUO","\xBB");
define("LDQUO","\x84");
define("RDQUO","\x93");
define("MDASH","\x97");
define("NDASH","\x96");
define("APOS","\xB4");
define("HELLIP","\x85");
// �������-��������� ��� �����
$Refs = array(); // ����� ��� �������� �����
$RefsCntr = 0; // ������� ������
function yyyTypo($x)
{
global $Refs, $RefsCntr;
$Refs[] = StripSlashes($x[0]);
return TAG1.($RefsCntr++).TAG2;
}
function zzzTypo($x)
{
global $Refs;
return $Refs[$x[1]];
}
function TypoAll($text, $isHTML = true)
{
global $Refs,$RefsCntr;
if($isHTML) {
$Refs = array(); // ���������� �����
$RefsCntr = 0; // ������� ������
/*
�������� ��������, ���������� ������� �� �� ������ ���������������:
�����������, �������, �����, ��� ���-������ - �� �����.
���������� ����� ���������� � Refs ��� ������ ������� xxxTypo()
*/
// �����������
$text = preg_replace_callback('{<!--.*?-->}s', 'yyyTypo', $text);
$PrivateTags = "title|script|style|pre|textarea";
$text = preg_replace_callback('{<\s*('.$PrivateTags.')[\s>].*?<\s*/\s*\1\s*>}is', 'yyyTypo', $text);
// ������� ����
$text = preg_replace_callback('{<(?:[^\'"\>]+|".*?"|\'.*?\')+>}s','yyyTypo',$text);
}
// ��. ������ ������� ���������
/*
������� �������: ������� ������ ��������� � ������!
������������� �������
����� �����������:
� ������ ������,
����� ������ "([{",
������
��������,
��� ����� �������
*/
$prequote = '\s\(\[\{";-';
$text = preg_replace('{^"}', LAQUO, $text);
$text = preg_replace('{(?<=['.$prequote.'])"}', LAQUO, $text);
// � ��� ��� ���, ��� �������� ������� �������
$text = preg_replace('{^((?:'.TAG1.'\d+'.TAG2.')+)"}', '\1'.LAQUO, $text);
$text = preg_replace('{(?<=['.$prequote.'])((?:'.TAG1.'\d+'.TAG2.')+)"}', '\1'.LAQUO, $text);
/*
������������� ������� - ��� ���������
�� �������� - � ��� �� ����� ������� ���� �����? ����� ������ - "!
*/
$text = str_replace('"', RAQUO, $text);
// ���������� ������ � ����������� ������� ���� ""... � ...""
// (������������, ��� �� ����� ����-��� ����� ������)
$text = preg_replace('{'.LAQUO.RAQUO.'}', LAQUO.LAQUO, $text);
$text = preg_replace('{'.RAQUO.LAQUO.'}', RAQUO.RAQUO, $text);
// ��������� ������
$i=0; // - ��� ������ �� ������������ (��� �������� � ������ ����������� ������������� �������)
while (($i++<10) && preg_match('{'.LAQUO.'(?:[^'.RAQUO.']*?)'.LAQUO.'}', $text))
$text = preg_replace(
'{'.LAQUO.'([^'.RAQUO.']*?)'.LAQUO.'(.*?)'.RAQUO.'}s',
LAQUO.'\1'.LDQUO.'\2'.RDQUO, $text);
$i=0;
while (($i++<10) && preg_match('{'.RAQUO.'(?:[^'.LAQUO.']*?)'.RAQUO.'}', $text))
$text = preg_replace(
'{'.RAQUO.'([^'.LAQUO.']*?)'.RAQUO.'}',
RDQUO.'\1'.RAQUO, $text);
// � ��������� ���������, ������� ������ ������������
// ����:
$text = preg_replace('{^-+(?=\s)}',MDASH,$text);
$text = preg_replace('{(?<=[\s'.TAG2.'])-+(?=\s)}',MDASH,$text);
$text = str_replace(' '.MDASH,' '.MDASH,$text);
// ndash:
$text = preg_replace('{(?<=\d)-(?=\d)}',NDASH,$text);
// ...:
$text = str_replace('...',HELLIP,$text);
// ��������:
$text = preg_replace('{(?<=\S)\'}',APOS,$text);
if($isHTML)
{
// ���������� ������ �������
while (preg_match('{'.TAG1.'.+?'.TAG2.'}', $text))
$text = preg_replace_callback('{'.TAG1.'(.+?)'.TAG2.'}', 'zzzTypo', $text);
}
// �������� ���� �������� �� HTML-entities.
$text = str_replace(
array(LAQUO,RAQUO,LDQUO,RDQUO,MDASH,NDASH,HELLIP,APOS),
array('«','»','„','“','—','–','…','’'),
$text
);
return $text;
}
?>