Jump to content

Regular Expressions


skerg

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. :)
Link to comment
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;

?>
Link to comment
Share on other sites

  • 2 months later...
[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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.