tags:

views:

69

answers:

4

I have a text link in a php file, but I want to make it an image link. The current code is.

$link="<a target='_blank' href='/stuff/morestuff/?url=".rawurlencode($url)."'>".t("Add")."</a>";

Now instead word "Add" I want to have an image in there. My current syntax to do that isn't working though, my error is Parse error: syntax error, unexpected T_STRING. Thanks.

+1  A: 

This should work

$link="<a target='_blank' href='/stuff/morestuff/?url=".rawurlencode($url)."'><img src='". $url_to_image ."' alt='' /></a>";

I am not sure what variable you have assigned to $url_to_image since t() looks like a custom function.

Garrett
A: 

First of all...the error must have been in your function "t". I am assuming that you have a function "t" :)

Here is the simplest way to put the link in instead of the word:

$link = "<a target='_blank' href='/stuff/morestuff/?url=" . rawurlencode($url) . "'><img src='http://www.myimage.com' /></a>";

Does that answer the question?

johnnietheblack
+1  A: 

You probably wrote double quotes inside your string without escaping them, like this:

t("<img src="http://asdf.com/jkl.png" />")

This would cause the T_STRING error that you mentioned. The correct way of writing this is:

t("<img src=\"http://asdf.com/jkl.png\" />")

Or you could use single quotes either for delimiting the string or the HTML tag attributes:

t("<img src='http://asdf.com/jkl.png' />")
t('<img src="http://asdf.com/jkl.png" />')
yjerem
A: 

The PHP code just generating HTML, which you want to look like..

<a target='_blank' href='/stuff/morestuff/?url=abcdef'><img src='image.jpg'></a>

To PHP'ify that, you'd do..

$link = "<a target='_blank' href='/stuff/morestuff/?url=".rawurlencode($url)."'><img src='".$url."'></a>";

..or, more tidily using sprintf:

$link = sprintf("<a target='_blank' href='/stuff/morestuff/?url=%s'><img src='$s'></a>",
    rawurlencode($url),
    $url
);
dbr