Page 1 of 1

eregi_replace

Posted: Mon Sep 26, 2005 5:00 am
by tkarven
hi, i have a simple eregi_coding as shown below.

Code: Select all

if(eregi("^(rm){1,1}(\ )?[0-9]+(\.)?[0-9]*[m|k]?$",$str)){
	$temp = $str;
	$tpower = 1;
	$temp = str_replace("rm","",$temp);
	$temp = str_replace(" ","",$temp);
	if(substr($temp,strlen($temp)-1) == "k") $tpower = 1000;
	if(substr($temp,strlen($temp)-1) == "m") $tpower = 1000000;
	$temp = str_replace("m","",$temp);
	$temp = str_replace("k","",$temp);
	echo "<br> temp ".$temp;
	echo "<br> tpower ".$tpower; 
	$temp = $temp * $tpower;
	echo "<br> total ".$temp; 
}
this coding works fine when we pass certain string only, e.g. rm 1.2m and it'll convert it to rm1200000. But what if we want to perform that out of a longer string which contains other information ?

For example i have a string like "bla bla bla bla rm 1.2m bla bla bla" which i want to convert it to "bla bla bla rm1200000 bla bla bla"

thanks in advance for any reply.

Posted: Mon Sep 26, 2005 5:51 am
by tkarven
okie found one solution which explode the long string with " " and pass each of the components for that function checks.

But still, this not gonna working if the price is separated by a space. for example , rm 1.2m will be passed as "rm" and "1.2m", which both not matching that eregi conditions.

Posted: Mon Sep 26, 2005 6:15 am
by ruchit
try having a look at preg_replace()... probably would be much better