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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading from txt, using single quote

Status
Not open for further replies.

wagnj1

Programmer
Jul 21, 2003
154
CA
on my site I'm reading a list of image files from a txt file and using that to create a list of hyperlinks that will link a user to that picture. however, I noticed that when a picture has a single quote in its name (i.e. "David's Vacation.jpg") it only reads up to the single quotation mark in the href attribute, which results in a dead link. How can I fix this?
 
escape the string before inserting it into quotes.

i'd use addslashes($string) for this.
 
no i wouldn't! i'd use rawurlencode() instead!

sorry - wasn't thinking straight.
 
thanks for the reply.

my code that is affected loooks like this:
Code:
echo "<a class='nav' href='new.php?picture=".rawurlencode($buffer)."'>".$buffer."</a><br />";
The address that it points to looks somewhat like this:
Code:
[URL unfurl="true"]http://www.domainname.com/images/new.php?picture=David%27s%20Photo.JPG%0D%0A[/URL]
However, it still will not link correctly to the picture.
 
thanks again. I've use rtrim() to remove the newline chars but it's still not loading. I have determined why but need some suggestions as per what to do:
My echo statement that puts the picture on the page is as follows:
Code:
echo "<img src='new/".$picture."' width=409 height=293 style='margin-left: 87px'>";

Notice how I'm using single quotes to enclose the src path. It seems to be hitting the single quote in the path name and thinking that is the end of it :
Code:
[COLOR=red]<img src=[/color][COLOR=blue]'new/David'[/color]s Photo.JPG'>

using rawurlencode() doesn't seem to help.
 
I'm not sure what to tell you. On my LAMP system, I have a test directory named "new", which contains two images named:

Bob's apple.jpg
Joe's peach.jpg

I have created a text file which reads:

Bob's apple.jpg<CR><LF>
Joe's peach.jpg<CR><LF>

The following script:
Code:
<?php
$fh = fopen ('foo.txt', 'r');
print '<html><body>';

while ($line = fgets($fh))
{
        $line = rtrim($line);
        print '<img src="new/' . rawurlencode ($line) . '"><br>
';
}

print '</body></html>';
?>

Outputs:


Code:
<html><body>
<img src="new/Bob%27s%20apple.jpg"><br>
<img src="new/Joe%27s%20peach.jpg"><br>
</body></html>

Which displays both images correctly.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Try changing echo statement

echo '<img src="new/'.$picture.'" width=409 height=293 style="margin-left: 87px">';

or

use print

print '<img src="new/' . rawurlencode ($line) . '">';

Let me know how it turns out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top