String Replace Problem

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
sanwar2007
Forum Newbie
Posts: 3
Joined: Thu Jul 24, 2008 7:06 am

String Replace Problem

Post 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
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: String Replace Problem

Post 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.
sanwar2007
Forum Newbie
Posts: 3
Joined: Thu Jul 24, 2008 7:06 am

Re: String Replace Problem

Post 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);
}
?>
sanwar2007
Forum Newbie
Posts: 3
Joined: Thu Jul 24, 2008 7:06 am

Re: String Replace Problem

Post by sanwar2007 »

Nobody is replying.
Please help it is very urgent for me.
Post Reply