ereg_replace

(PHP 4, PHP 5)

ereg_replace — ������ʽ�滻

˵��

ereg_replace ( string $pattern , string $replacement , string $string ) : string

�������� string ��ɨ���� pattern ƥ��IJ��֣��������滻Ϊ replacement��

�����滻����ַ����������û�пɹ��滻��ƥ������᷵��ԭ�ַ�������

��� pattern �����������ڵ��Ӵ����� replacement ���԰������� \\digit ���Ӵ�����Щ�Ӵ������滻Ϊ���ֱ�ʾ�ĵĵڼ��������ڵ��Ӵ���\\0 ��������ַ������������ݡ��������þŸ��Ӵ������ſ���Ƕ�ף�������������Բ����������˳��

���δ�� string ���ҵ�ƥ����� string ��ԭ�����ء�

���磬����Ĵ���Ƭ����� "This was a test" ���Σ�

Example #1 ereg_replace() ����

<?php
$string 
= "This is a test";
echo 
str_replace(" is", " was", $string);
echo 
ereg_replace("( )is", "\\1was", $string);
echo 
ereg_replace("(( )is)", "\\2was", $string);
?>

Ҫע���һ��������� replacement ������ʹ��������ֵ������ܵò����������Ľ����������Ϊ ereg_replace() ����������Ϊ�ַ�������ֵ�����Ͳ�Ӧ��֮�����磺

Example #2 ereg_replace() ����

<?php
/* ���ܲ����������Ľ�� */
$num = 4;
$string = "This string has four words.";
$string = ereg_replace('four', $num, $string);
echo 
$string;   /* Output: 'This string has   words.' */

/* ������������ */
$num = '4';
$string = "This string has four words.";
$string = ereg_replace('four', $num, $string);
echo 
$string;   /* Output: 'This string has 4 words.' */
?>

Example #3 �� URL �滻Ϊ������

<?php
$text 
= ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                     
"<a href=\"\\0\">\\0</a>", $text);
?>

Tip

preg_replace() ����ʹ���� Perl ����������ʽ�﷨��ͨ���DZ� ereg_replace() ��������������

�μ� ereg()��eregi()��eregi_replace()��str_replace() �� preg_match()��