search for in the  
<eregeregi>
Last updated: Thu, 19 May 2005

eregi_replace

(PHP 3, PHP 4, PHP 5)

eregi_replace -- Replace regular expression case insensitive

Description

string eregi_replace ( string pattern, string replacement, string string )

This function is identical to ereg_replace() except that this ignores case distinction when matching alphabetic characters.

Example 1. Highlight search results

<?php
$pattern
= '(>[^<]*)('. quotemeta($_GET['search']) .')';
$replacement = '\\1<span class="search">\\2</span>';
$body = eregi_replace($pattern, $replacement, $body);
?>

See also ereg(), eregi(), and ereg_replace().



User Contributed Notes
eregi_replace
joltmans at amersel dot com
01-Mar-2005 01:06
PHP's Regex engine differs from several others in its treatment of parsing spaces.

In many Regex languages '\s' denotes a space.
PHP does not recognize '\s', just type a space ' ' instead.

This simple example illustrates the problem:
<?php
   $string
= "A sentence with  spaces";
   if (
eregi("with\s*spaces", $string))
   {
      
// Will never print
      
echo "PHP understood \s";
   }
   else
   {
      
// Will always print
      
echo "PHP doesn't understand \s";
   }
?>

This example does work:
<?php
   $string
= "A sentence with  spaces";
   if (
eregi("with *spaces", $string))
   {
      
// Will print
      
echo "PHP understood ' '";
   }
?>
27-Jan-2005 10:15
never mind my last post for the eregi_replace not replacing.

I just used str_replace instead and it works fine.  I must have had something wrong with my search string.  POSIX, Perl.. hmm.. yeah probably something there.

Mettedraq / Gene
Ausvald
19-Jun-2004 06:21
Zach Johnson missed up. ereg* funcs use posix regex, not the rfc one
furiousgreencloud at yahoo dot com
17-Jun-2004 10:47
To simply convert wild input into a sensable sting, say for a filename I use:

function cleanString($wild) {
   return ereg_replace("[^[:alnum:]+]","",$wild);
}
                                                                              
echo cleanString("@#$&*$@#H~e'{}l{}l<o\{}"); // outputs: Hello
e dot boeters at planet dot nl
03-Jun-2004 06:46
This is a 'faster' way to highlight search results:

$content = str_replace($query, "<span class=\"highlight\">" . $query . "</span>", $content);
julien at ratatouille dot com dot fr
24-Nov-2003 08:28
This function replace < and > symbols between <code> and </code> tags by html code for lower than (&lt;) and greather than (&gt;) elements.

function retourne_format_code($texte)
{
   $tablo=split("<code>",$texte);
   $texte="";
   $texte.=$tablo[0];
   foreach($tablo as $cle=>$valeur)
   {
       if(eregi("</code>",$valeur))
       {
           $tablo1=split("</code>",$valeur);
           $tablo1[0]=eregi_replace("<","&lt;",$tablo1[0]);
           $tablo1[0]=eregi_replace(">","&gt;",$tablo1[0]);
           foreach($tablo1 as $cle1=>$valeur1)
           {
               if($cle1==0)
                   $valeur1="<code>".$valeur1."</code>";
               $texte.=$valeur1;
           }
       }       
   }
   return $texte;
}
martin_goldmann at gmx dot net
18-Nov-2003 02:43
After reading the last message I wrote that de-spamizer:

function despamMailURI($myStrMail='')
{
   ?>javascript:void(location.href='mailto:'+<?=eregi_replace( "^([_\.0-9a-z-]+)@([0-9a-z][0-9a-z-]+)\.([a-z]{2,6})$", "'\\1'+'@'+'\\2'+'.'+'\\3'",$myStrMail)?>)<?
}

Usage example:
<
a href="<?=despamMailURI("user@foo.bar")?>">Mail me</a>
iparanoid at gmx dot de
23-Aug-2003 11:03
To obtain the an email addresse in the scheme user at host dot use following function

function antispam_mail($mail) {
   return eregi_replace( "^([_\.0-9a-z-]+)@([0-9a-z][0-9a-z-]+)\.([a-z]{2,6})$", '\\1 at \\2 dot \\3',$mail );
};

Combined with a wee JavaScript (document.location='mailto:'+user+'@'+host+'.'+tld) this provides a very powerfull anti-spam mechanism while providing full mailto: link functionality.
chpins-php at romu92 dot freesurf dot fr
02-Jun-2003 06:15
/*new function for href2text : */

function AHREF2text($string) {
 return eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>].*>([^<]*)</a>)','[\\3] (link: \\2)', $string);
}

// by Ch'Pins
buddy at directpay dot cz
14-May-2003 12:22
i had to solve problem conserning DB2 timestamp format. here is how to parse ANSI timestamp format to DB2 timestamp format:

$mydate = Date("Y-m-d H:i:s"); 
 
$var = eregi_replace
("([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})"
,"\\1-\\2.\\3.\\4",$mydate);

echo "ANSI: $mydate, DB2 format: $var";

happy codding
buddy
eder at xce dot de
03-May-2003 09:23
/*As php at silisoftware dot com's example works only if there is not more than one link in $string, I rewrote his expression to function with strings containing multiple links: */

function AHREF2text($string) {
  return eregi_replace('(<a [^<]*href=["|\']?([^ "\']*)["|\']?[^>]*>([^<]*)</a>)','[\\3] (link: \\2)', $string);
}
Rainmaker526 at hotmail.com
09-Mar-2003 09:02
I have found that some characters cannot be used by eregi_replace (or ereg_replace). When you get the REG_BADRPT error, try backslashing any special chars in your pattern string

ex.
$str = eregi_replace("*", "", $somevar)

gives the REG_BADRPT error. Change it to
$str = eregi_replace("\*", "", $somevar)

to make it work
php at silisoftware dot com
18-Feb-2003 01:08
Transform HTML links into plain-text "links" with the URL visible

function AHREF2text($string) {
   return eregi_replace('<A .*HREF=("|\')?([^ "\']*)("|\')?.*>([^<]*)</A>', '[\\4] (link: \\2)', $string);
}

$HTMLstring = 'A link to <a href="http://www.php.net">PHP.net</A>';
echo AHREF2text($HTMLstring);
// prints:  A link to [PHP.net] (link: http://www.php.net)
simon_a99 at yahoo dot co dot uk
27-Aug-2002 02:07
To find a string regardless of case, you might want to use the matched string in the replacement string without changing its case.

For example, you're searching for $search = "letter" and the text being searched is $text = "post lEtTeR".  I want to change the format of the matched string.

Do this:
eregi_replace($search, "<b>\\0</b>", $text);

$text has now been changed to "post <b>lEtTeR</b>".  \\0 is the entire text matched (lEtTer in this case).
shane at unixcentral dot co dot za
07-Feb-2002 07:53
A Quick way of removing excess spaces:
<?
$string
= "One        Two          Three  Four";

$var = eregi_replace(" +", " ", $string);

echo
$var;
?>

<eregeregi>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT