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
String Replace Problem
Moderator: General Moderators
Re: String Replace Problem
You could do a negative look-behind regexp:
The (?<! ) is the negative look-behind group.
Code: Select all
$output = preg_replace('#(?<!-)Edu-Cess#', '0', $input);-
sanwar2007
- Forum Newbie
- Posts: 3
- Joined: Thu Jul 24, 2008 7:06 am
Re: String Replace Problem
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);
}
?>
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);
}
?>
-
sanwar2007
- Forum Newbie
- Posts: 3
- Joined: Thu Jul 24, 2008 7:06 am
Re: String Replace Problem
Nobody is replying.
Please help it is very urgent for me.
Please help it is very urgent for me.