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!

anybody encountered this problem

Status
Not open for further replies.

kaancho12

Technical User
Feb 22, 2005
191
hi,
i guess this str_replace doesnt really work when the content is not delimited by line. let me explain:
i have this "html" file with all the contents dumped without any separation and when trying to execute str_replace -it fails to find the pattern and thus fails...is there a solution to this??
here's an example of what i am trying to do:
The string I am trying to find:
<A HREF=\"/ABC/10to99/pdf/ABC20.pdf\">20</A>
The string i am trying to replace with:
<A HREF=\"/ABC/10to99/pdf/ABC20.pdf\">20</A><input type=\"button\" name=\"button_20\" onClick=\"Locat(\'ABC/html/10to99/ABC20.html/\');return false\">

The HTML file is all in one line and probably can be broken through regex but i was wondering if there were other solutions
thanks ko12
 
This code works fine.
Code:
<?
$str = '<A HREF="/ABC/10to99/pdf/ABC20.pdf">20</A>';
$rep_str = '<A HREF="/ABC/10to99/pdf/ABC20.pdf">20</A>' . 
        '<input type="button" name="button_20" onClick="Locat('.
        "'ABC/html/10to99/ABC20.html/'".');return false">';
$old_str = $str . $str . $str .$str .$str;
$new_str = str_replace($str,$rep_str,$old_str);
echo 'Old String: ' . $old_str . "\n\n";
echo 'New String: ' . $new_str;
?>
And produces:
Code:
$ php -q str_rep.php
Old String: <A HREF="/ABC/10to99/pdf/ABC20.pdf">20</A><A HREF="/ABC/10to99/pdf/ABC20.pdf">20</A><A 
HREF="/ABC/10to99/pdf/ABC20.pdf">20</A><A HREF="/ABC/10to99/pdf/ABC20.pdf">20</A><A HREF="/ABC/10to
99/pdf/ABC20.pdf">20</A>

New String: <A HREF="/ABC/10to99/pdf/ABC20.pdf">20</A><input type="button" name="button_20" onClick
="Locat('ABC/html/10to99/ABC20.html/');return false"><A HREF="/ABC/10to99/pdf/ABC20.pdf">20</A><inp
ut type="button" name="button_20" onClick="Locat('ABC/html/10to99/ABC20.html/');return false"><A HR
EF="/ABC/10to99/pdf/ABC20.pdf">20</A><input type="button" name="button_20" onClick="Locat('ABC/html
/10to99/ABC20.html/');return false"><A HREF="/ABC/10to99/pdf/ABC20.pdf">20</A><input type="button" 
name="button_20" onClick="Locat('ABC/html/10to99/ABC20.html/');return false"><A HREF="/ABC/10to99/p
df/ABC20.pdf">20</A><input type="button" name="button_20" onClick="Locat('ABC/html/10to99/ABC20.htm
l/');return false">

I think part of your problem is using the back-slash to escape quotes too much. Just use double quotes to enclose strings that include single quotes and single quotes to enclose strings that include double quotes. Then put them in a single string.

Ken
 
hi kenrbnsn,
thanks for the reply. i tried what you have above and it worked for me too...but when i tried the same code on the file it failed. here's the file i am trying to replace the string in:
//code
<?
$contents = "<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>";

$str = '<A HREF=\"/abc/specs/10to99/pdf/abc20.pdf\">20</A>';
$rep_str = '<A HREF=\"/abc/specs/10to99/pdf/abc20.pdf\">20</A>' .
'<input type="button" name="button_20" onClick="Locat('.
"'abc/html/10to99/ABC20.html/'".');return false">';
$new_str = str_replace($str,$rep_str,$contents);
echo "new string: " . $new_str . "\n";
?>
//end of code
the string i am trying to replace is at the end of the $content.
any indea how to make this work???
thanks again,
ko12
 
hi,
never mind. needed "double quotes" in the search and replace string instead of single one.
thanks
ko12
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top