Page 1 of 1

String Replace Problem

Posted: Thu Jul 24, 2008 7:19 am
by sanwar2007
I have the following code
$formula=((( Basic) * ( 1 + Cess Duty + Excise Duty * ( 1 + Edu-Cess + Sec -Edu-Cess) + Packing & Forwarding) + Packing & Forwarding fixed)+ Commission) * CST against C Form

If i replace 'Edu-Cess' by '0' in the above string using str_replace function then the string becomes

$formula=((( Basic) * ( 1 + Cess Duty + Excise Duty * ( 1 + 0 + Sec -0) + Packing & Forwarding) + Packing & Forwarding fixed)+ Commission) * CST against C Form

I the above string 'Edu-Cess' gets replaced by '0' but also in 'Sec -Edu-Cess' it gets replaced and 'Sec -0' remains which is meaningless.
I want to replace only full string.
Is there any way to replace this using regular expression?
Thanks in advance.

Sandeep

Re: String Replace Problem

Posted: Thu Jul 24, 2008 7:29 am
by manixrock
You could do a negative look-behind regexp:

Code: Select all

$output = preg_replace('#(?<!-)Edu-Cess#', '0', $input);
The (?<! ) is the negative look-behind group.

Re: String Replace Problem

Posted: Fri Jul 25, 2008 12:34 am
by sanwar2007
Thanks for the help.
The formula is fetched from database. So we don't know the exact name of the tax. After that tax names are fetched from database & replaced with 0. My code is like this. As per id Edu-Cess is fetched before than 'Sec -Edu-Cess' in $chk_sql, so the str_replace will replace 'Edu-Cess' in both i.e. 'Edu-Cess' & 'Sec -Edu-Cess'. The same happens in case of 'Packing & Forwarding' & 'Packing & Forwarding fixed'.
<?php
$formula=((( Basic) * ( 1 + Cess Duty + Excise Duty * ( 1 +
Edu-Cess + Sec -Edu-Cess) + Packing & Forwarding)
+ Packing & Forwarding fixed)+ Commission) * CST
against C Form
$chk_sql="SELECT tax_name FROM 52tax_master WHERE
tax_id NOT IN ($tax_idstr) AND deleted='n'";

$chk_query=mysql_db_query($dbname,$chk_sql,$link);

while($chk_row=mysql_fetch_array($chk_query))
{
$formula=str_replace($chk_row[tax_name],0,$formula);
}
?>

Re: String Replace Problem

Posted: Sat Jul 26, 2008 6:32 am
by sanwar2007
Nobody is replying.
Please help it is very urgent for me.