Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with str_replace() when using with mysql 1

Status
Not open for further replies.

kaancho12

Technical User
Feb 22, 2005
191
hi,
this is a follow up from my earlier post about str_replace.
When i do a regular str_replace using the following it works fine:
$search = "<A HREF=\"/ABC/specs/10to99/pdf/abc16.pdf\">16</A>";
$replace = "<A HREF=\"/ABC/specs/10to99/pdf/abc16.pdf\">16TTTT</A>";
str_replace($search, $replace, $contents);
But when the $search, $replace are obtained through a mysql query then they dont seem to work.
while ($fetch = mysql_fetch_array($sql)){
$search = '"' . $fetch["preg"] . '"';
$replace = '"' . $fetch["preg"] . $fetch["pregReplace"] . '"';
$new_str = str_replace($search, $replace, $contents);
}
here, $fetch["preg"] and $fetch["pregReplace"] are results of a mysql query. This doesnt seem to work ??????
any idea why?????
thanks in advance
ko12
 
can you post the text strings of $search and $replace? also i'm not sure about your use of quotes. this oculd be causing the problems. it might be easier to use this while segment:
Code:
while ($fetch = mysql_fetch_array($sql)){
$search = $fetch["preg"];
$replace = $fetch["preg"] . $fetch["pregReplace"];
$new_str = str_replace($search, $replace, $contents);
}
 
here are the text strings.
$fetch["preg"] gets this:
<A HREF="/ABC/specs/10to99/pdf/abc16.pdf">16</A>
$fetch["pregReplace"] gets this:
<input type="button" name="button_16" onClick="Locat('/ABC/html/10to99/ABC16.html/');return false">

jpadie, thank you for your help in the other post.(actually i tried using the str_replace which you wrote but it did not work so i resorted to entering the data i received from preg_match into mysql database and then doing the str_replace and thats when i ran into the trouble above.
////// From your code Yesterday ////
//first read all the file into a string
foreach ($button as $key=>$val)
{
str_replace ($key."<\a>", $key."<\a>".$val, $contents); //ie replace with itself plus the new button code
}
$newfilename = "parsed_".$filename;
$fh=fopen($newfilename,"wb");
fwrite($fh, $contents);
fclose ($fh);
/////////////////////////////////////

If i echo the str_replace -it shows up fine and if i copy individual str_replace from that to a separate file and run the program -- it works fine but it doesnt work when i run str_replace in a while ($fetch) loop.
So, i am confused as to why this is happening.
 
Hi

in your code posted above, the quotes were incorrect and this will be having an effect on the search string.

could you retry with the code i posted just above (which fixes the quotes issue).

you might also want to escape the strings before using them in str_replace. use addslashes() to do this. this will be having a negative effect too.

in what way did the code of yesterday "not work"? it may be easier to fix it that way - it seems a bit clunky to read a file into a database!

Justin
 
in what way did the code of yesterday "not work"? it may be easier to fix it that way - it seems a bit clunky to read a file into a database!"
this is what i had:
//first read all the file into a string
foreach ($button as $key=>$val)
{
$mat = ">" . $key . "</A>" ;
$rep = ">" . $key . "</A>" . $val ;
$contentnew = str_replace($mat, $rep, $contents);
echo "str_replace($mat, $rep, $contents)";
}
echo $contentnew;
//
When i viewed "$contentnew" it was same as "$contents". The "echo "str_replace($mat, $rep, $contents)" though did give an output for individual "$button" and when i copied an individual button and ran it in a separate file it worked fine.

"could you retry with the code i posted just above (which fixes the quotes issue)."
I did retry the code without the '"' with the same result.
Thank u for helping out,
ko12
 
did you also escape the strings?

could you post the value of the echo "str_replace" command?

 
here's the echo of str_replace:
str_replace(>20</A>, >20</A><input type=\"button\" name=\"button_20\" onClick=\"Locat(\'/ABC/html/10to99/ABC20.html/\');return false\">, <html><head></td><td width=\"78%\" height=\"379\" align=\"left\" valign=\"top\"> <DIV ALIGN=\"CEABCR\"><FONT SIZE=\"+2\"><B><I>Silicon Transistor Selector Guide</I></B></FONT><BR><B>(Listed in Order of Package and BV<SUB>CBO</SUB> Rating, *T<SUB>C</SUB> = +25&#176;C)</B></DIV><TABLE WIDTH=\"100%\" BORDER=\"1\"><TR ALIGN=\"CEABCR\" VALIGN=\"BOTTOM\"><TD COLSPAN=\"2\"><B><FONT SIZE=\"-1\">ABC Type<BR>Number</FONT></B></TD><TD><FONT SIZE=\"-1\"><B>Application</B></FONT></TD><TD COLSPAN=\"2\"><FONT SIZE=\"-1\"><B>Maximum<BR>Breakdown Voltage</B><BR>(Volts)</FONT></TD><TD><FONT SIZE=\"-1\"><B>Maximum<BR>Collector<BR>Current</B><BR>(Amps)</FONT></TD><TD><FONT SIZE=\"-1\"><B>Maximum<BR>Collector<BR>Dissipation</B><BR>(Watts)</FONT></TD><TD><FONT SIZE=\"-1\"><B>Typical<BR>Forward<BR>Current<BR>Gain</B></FONT></TD><TD><FONT SIZE=\"-1\"><B>Typical<BR>Freq</B><BR>(MHz)</FONT></TD><TD><FONT SIZE=\"-1\"><B>Package Style</B></FONT></TD></TR><TR ALIGN=\"CEABCR\" VALIGN=\"BOTTOM\"><TD><FONT SIZE=\"-1\"><B>NPN</B></FONT></TD><TD><FONT SIZE=\"-1\"><B>PNP</B></FONT></TD><TD><FONT SIZE=\"-1\"><B></B></FONT></TD><TD><FONT SIZE=\"-1\"><B>BV<SUB>CBO</SUB></B></FONT></TD><TD><FONT SIZE=\"-1\"><B>BV<SUB>CEO</SUB></B></FONT></TD><TD><FONT SIZE=\"-1\"><B>I<SUB>C</SUB></B></FONT></TD><TD><FONT SIZE=\"-1\"><B>P<SUB>D</SUB></B></FONT></TD><TD><FONT SIZE=\"-1\"><B>h<SUB>FE</SUB></B></FONT></TD><TD><FONT SIZE=\"-1\"><B>f<SUB>T</SUB></B></FONT></TD><TD><FONT SIZE=\"-1\"><B></B></FONT></TD></TR><TR ALIGN=\"CEABCR\" VALIGN=\"TOP\"><TD><FONT SIZE=\"-1\"><A HREF=\"/ABC/specs/10to99/pdf/ABC20.pdf\">20</A></FONT></TD><TD><FONT SIZE=\"-1\"><A HREF=\"/ABC/specs/10to99/pdf/ABC20.pdf\">21</A></FONT></TD><TD><FONT SIZE=\"-1\">AF PO, Dr, Series Pass</FONT></TD><TD><FONT SIZE=\"-1\">40</FONT></TD><TD><FONT SIZE=\"-1\">32</FONT></TD><TD><FONT SIZE=\"-1\">2</FONT></TD><TD><FONT SIZE=\"-1\">1</FONT></TD><TD><FONT SIZE=\"-1\">120 Min</FONT></TD><TD><FONT SIZE=\"-1\">100</FONT></TD><TD ALIGN=\"LEFT\" ROWSPAN=\"4\"><IMG SRC=\"/graphics/BiPo53.gif\"></TD></TR><TR ALIGN=\"CEABCR\" VALIGN=\"TOP\"><TD><FONT SIZE=\"-1\"><A HREF=\"/ABC/specs/10to99/pdf/ABC16.pdf\">16</A></FONT></TD><TD><FONT SIZE=\"-1\"><A HREF=\"/ABC/specs/10to99/pdf/ABC16.pdf\">17</A></FONT></TD><TD><FONT SIZE=\"-1\">Low Noise Gen Purp Amp</FONT></TD><TD><FONT SIZE=\"-1\">50</FONT></TD><TD><FONT SIZE=\"-1\">40</FONT></TD><TD><FONT SIZE=\"-1\">0.1</FONT></TD><TD><FONT SIZE=\"-1\">0.3</FONT></TD><TD><FONT SIZE=\"-1\">270 Min</FONT></TD><TD><FONT SIZE=\"-1\">180/140</FONT></TD></TR><TR ALIGN=\"CEABCR\" VALIGN=\"TOP\"><TD><FONT SIZE=\"-1\"><A HREF=\"/ABC/specs/10to99/pdf/ABC18.pdf\">18</A></FONT></TD><TD><FONT SIZE=\"-1\"><A HREF=\"/ABC/specs/10to99/pdf/ABC18.pdf\">19</A></FONT></TD><TD><FONT SIZE=\"-1\">High Breakdown Vltg<BR>Large Current Capacity Driver</FONT></TD><TD><FONT SIZE=\"-1\">80</FONT></TD><TD><FONT SIZE=\"-1\">80</FONT></TD><TD><FONT SIZE=\"-1\">0.7</FONT></TD><TD><FONT SIZE=\"-1\">1</FONT></TD><TD><FONT SIZE=\"-1\">120 Min</FONT></TD><TD><FONT SIZE=\"-1\">120/100</FONT></TD></TR><TR ALIGN=\"CEABCR\" VALIGN=\"TOP\"><TD><FONT SIZE=\"-1\"><A HREF=\"/ABC/specs/10to99/pdf/ABC22.pdf\">22</A></FONT></TD><TD><FONT SIZE=\"-1\">-</FONT></TD><TD><FONT SIZE=\"-1\">AF PO, Dr, Gen Purp Amp</FONT></TD><TD><FONT SIZE=\"-1\">100</FONT></TD><TD><FONT SIZE=\"-1\">80</FONT></TD><TD><FONT SIZE=\"-1\">1</FONT></TD><TD><FONT SIZE=\"-1\">0.9</FONT></TD><TD><FONT SIZE=\"-1\">120 Min</FONT></TD><TD><FONT SIZE=\"-1\">100</FONT></TD></TR><TR ALIGN=\"CEABCR\" VALIGN=\"TOP\"><TD><FONT SIZE=\"-1\">302</FONT></TD><TD>
</table>
</body>
</html>
)
 
thanks.

i've tested this myself - a direct cut and paste from the code above, add double quotes around each function parameter, echo the results and it works just fine.

not sure why it is not working on your system!

Justin
 
thanks for your input. i guess i will keep on cranking this :)
ko12
 
just a thought -

are magic_quotes_runtime and magic_quotes_gpc switched off?

 
hi,
i havent checked those but -- :))))i got the script to work finally...had some mistakes but overall,, what u have given me worked find --just needed to think through the process of how the data was coming in :)))
here's the script in the end:
///////////////////////////////////////////////////////////
$contentnew = $contents ;
foreach ($button as $key=>$val)
{
if ($key > 1){
//echo "key: " . $key . " val: " . $val . "\n";
$mat = ">" . $key . "</A>" ;
$rep = ">" . $key . "</A>" . $val ;
$contentnew = str_replace("$mat", "$rep", $contentnew);
}
}
///////////////////////////////////////////////////////////
thanks a lot for your help,
ko12
 
oh dear!

that was rather a basic error from me. How embarrassing!

comes of only doing one debug cycle i guess!

glad you're up and running!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top