ereg_replace

(PHP 4, PHP 5)

ereg_replace锟斤拷锟斤拷锟斤拷式锟芥换

说锟斤拷

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

锟斤拷锟斤拷锟斤拷锟斤拷 string 锟斤拷扫锟斤拷锟斤拷 pattern 匹锟斤拷牟锟斤拷郑锟斤拷锟斤拷锟斤拷锟斤拷婊晃� replacement锟斤拷

锟斤拷锟斤拷锟芥换锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷没锟叫可癸拷锟芥换锟斤拷匹锟斤拷锟斤拷锟斤拷岱碉拷锟皆拷址锟斤拷锟斤拷锟斤拷锟�

锟斤拷锟� pattern 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟节碉拷锟接达拷锟斤拷锟斤拷 replacement 锟斤拷锟皆帮拷锟斤拷锟斤拷锟斤拷 \\digit 锟斤拷锟接达拷锟斤拷锟斤拷些锟接达拷锟斤拷锟斤拷锟芥换为锟斤拷锟街憋拷示锟侥的第硷拷锟斤拷锟斤拷锟斤拷锟节碉拷锟接达拷锟斤拷\\0 锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷荨锟斤拷锟斤拷锟斤拷锟斤拷镁鸥锟斤拷哟锟斤拷锟斤拷锟斤拷趴锟斤拷锟角讹拷祝锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿筹拷锟�

锟斤拷锟轿达拷锟� string 锟斤拷锟揭碉拷匹锟斤拷锟筋,锟斤拷 string 锟斤拷原锟斤拷锟斤拷锟截★拷

锟斤拷锟界,锟斤拷锟斤拷拇锟斤拷锟狡拷锟斤拷锟斤拷 "This was a test" 锟斤拷锟轿o拷

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 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷式锟斤法锟斤拷通锟斤拷锟角憋拷 ereg_replace() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

锟轿硷拷 ereg()锟斤拷eregi()锟斤拷eregi_replace()锟斤拷str_replace() 锟斤拷 preg_match()锟斤拷