Jump to content


Photo

Regular Expressions


  • Please log in to reply
2 replies to this topic

#1 skerg

skerg
  • New Members
  • Pip
  • Newbie
  • 2 posts
  • Location/tmp/utah

Posted 07 July 2006 - 10:00 PM

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. :)

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 07 July 2006 - 10:06 PM

<?

//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;

?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 08 September 2006 - 02:49 PM

<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>

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


Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users