eregi_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tkarven
Forum Commoner
Posts: 41
Joined: Tue Aug 02, 2005 10:26 pm

eregi_replace

Post 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.
tkarven
Forum Commoner
Posts: 41
Joined: Tue Aug 02, 2005 10:26 pm

Post 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.
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post by ruchit »

try having a look at preg_replace()... probably would be much better
Post Reply