Jump to content

Archived

This topic is now archived and is closed to further replies.

skerg

Regular Expressions

Recommended Posts

Hey guys, I've started to play with some regular expressions but for the life of me I can't figure out the correct syntax for this. I need a regex that can find different versions of the word Center. For example I need to be able to change Ctr and Cntr into Center. I can't seem to figure out how to do it, if anyone can help me out I'd be very thankfull. :)

Share this post


Link to post
Share on other sites
<?

//This is erigi replace with no array.

$a="Cnt";

$b ="center";

$replace=eregi_replace($a,"",$b);

echo $replace;

?>

<?

//This is eregi with array

$a=array("Cnt");

$b=array("center");

$replace=eregi_replace($a[0],"",$b[0]);

echo $replace;

?>


<?

//This is str_replace with no array.

$a="Cnt";

$b ="center";

$replace=str_replace($a,"",$b);

echo $replace;

?>

<?

//This is str_replace with array

$a=array("Cnt");

$b=array("center");

$replace=str_replace($a[0],"",$b[0]);

echo $replace;

?>

Share this post


Link to post
Share on other sites
[code]
<pre>
<?php
$tests = array(
'center',
'CENTER',
'CeNtEr',
'cntr',
'ctr',
'CTr',
'g+a-r1bag#e',
'string with ctr',
);

foreach ($tests as $test) {
echo "$test => ";
echo preg_replace('/ce?n?te?r/i', 'Center', $test);
echo '<br/>';
}
?>
</pre>
[/code]
[quote]
center => Center
CENTER => Center
CeNtEr => Center
cntr => Center
ctr => Center
CTr => Center
g+a-r1bag#e => g+a-r1bag#e
string with ctr => string with Center
[/quote]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.