How to echo html with variables in herf? - php

I tried to echo the following html, I can see from firebug that the variables are correctly printed, but it doesn't show up on the webpage. Would anyone please help me correct this?
$html ="<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank' $img></a></p>";

Your $img is in the wrong spot:
$html ="<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank'>$img</a></p>";
Then again I think you want to do this:
$html ="<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank'><img src='$img' /></a></p>";

There is nothing between the and the tags. you need something like this:
<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank'> $img </a></p>

Related

What' wrong with this syntax?

I am sure this is simple, but I don't get why...
Why is this wrong?
echo "<img src='".bloginfo('template_directory')."/systemdata/next.png' border=0 id='NavImage'>";
If I do it like this, it works:
echo "<img src='";
echo bloginfo('template_directory');
echo "/systemdata/next.png' border=0 id='NavImage'>";
What do I do not get? I want to write nice code and the second example is not very elegant I think.
Thanks!
You are using the wrong function, bloginfo() already outputs / echoes so you cannot use it when you want to concatenate strings (nor should you echo it...).
Instead you could use get_bloginfo() as that returns a string:
echo "<img src='".get_bloginfo('template_directory')."/systemdata/next.png' border=0 id='NavImage'>";
You can try this as far as code elegant factor is concerned:
$blogingo=bloginfo('template_directory');
echo "<img src=$blogInfo/systemdata/next.png' border=0 id='NavImage'>";

IMG SRC Using Path from MySQL and PHP

I have the following line of code which doesn't seem to be working:
echo "<img src='/images/albumart'"; echo $row['art1']; echo "/>";
The 'art1' is definitely returning '/imagename.png' so the URL should be complete however nothing being displayed.
Any ideas?
Thanks.
You really should be concatenating that string:
<?php
echo '<img src="/images/albumart'.$row['art1'].'"/>';
?>
and ideally break out of php to do this:
<img src="/images/albumart<?php echo $row['art1']; ?>"/>
If you're not familiar, the period in php joins strings. That's called concatenation. No need to echo little bits like that, in fact please never do it the way you demonstrate. The root of your issue is, as Geoff pointed out, you weren't closing the src param.
echo "<img src='/images/albumart'"; echo $row['art1']; echo "/>";
It looks like you are closing the img src paramter after the albumart part.,
try this:
echo "<img src='/images/albumart"; echo $row['art1']; echo "' />";

How do I use an IF statement in PHP to decide if I should show an image or not?

I'm setting up a weather page using the wunderground.com API. Anyway, I want to do the following:
Of the variable $weather is equal to Clear, I want to display an image.
I've tried this:
if ($weather=="Clear") echo <img src="http://example.org/clear.gif" alt="Clear"/>
What do I need to do instead?
Try this
if ($weather=="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>';
The code you tried will render ERROR. You need to place the HTML text and any string within quotes, before you echo them.
if ($weather=="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>'
Remaining, there is nothing else to improve :)
if ($weather==="Clear")
echo "<img src=\"http://example.org/clear.gif\" alt=\"Clear\"/>";
echo '<img src="http://example.org/clear.gif" alt="Clear"/>';
OR
echo "<img src='http://example.org/clear.gif' alt='Clear' />";
if ($weather=="Clear") echo "<img src=\"http://example.org/clear.gif\" alt=\"Clear\"/>";
OR
if ($weather==="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>';
OR
if ($weather==="Clear") echo <<<ABC
<img src="http://example.org/clear.gif" alt="Clear"/>
ABC;
andsoon.
For conditionally rendering html, I often just use the php tags...
if($weather === "Clear") {?>
<img src="http://example.org/clear.gif" alt="Clear"/>
<?php}

How to Add PHP-Code to an attribute using DOM on PHP?

How can I do somethink like this with PHP DOM?
<img src="<?php echo $picsrc; ?>">
This Code
$node->setAttribute('src','<?php echo $picsrc;?>');
does escape the PHP Tag.
<img src="<?php echo $picsrc; ?>">
Is there a way to use
$dom->createProcessingInstruction()
for Attribute Values?
You should try this
$srcPath = '<?php echo $picsrc;?>';
$node->setAttribute('src', html_entity_decode($srcPath));
#thecatontheflat was right, decoding is the Answer!
But using it this way:
$node->setAttribute('src','<?php echo $picsrc; ?>');
$doc->save('dynamic.php');
$html = htmlspecialchars_decode(file_get_contents('dynamic.php'));
$html = html_entity_decode($html);
file_put_contents('dynamic.php',$html);
Don't try to avert the conversion, just revoke it. ;)
thanks at all.

Preg_match - retrieving href

I have got stuck with a question I have just been helped on - its a new problem but only just slightly.
I have this preg_match to get the contents of href. Please don't tell me not to use regex - I am aware of using other parsers/classes etc but this is an old script that just needs to be fixed for now. :) No time for re-writes!
preg_match("~<a target=\'_blank\' rel=\'nofollow\' href=\"(.*?)\">~i", $epilink, $epiurl);
It returns:
http://www.example.com/frame2.php?view=&epi=54673-r
However, it should return:
http://www.example.com/frame2.php?view=168204&epi=54673
This is an example of html it would work with:
<a target='_blank' rel='nofollow' href="http://www.example.com/frame2.php?view=545903&epi=54683">
Why is the URL I have returned malformed?
Thanks all for any help.
$string="<a target='_blank' rel='nofollow' href=\"http://www.example.com/frame2.php?view=545903&epi=54683\">";
$s = explode('">',$string);
foreach($s as $k){
if (strpos($k,"href")!==FALSE){
echo preg_replace('/.*href="|/ms',"",$k);
break;
}
}
output
$ php test.php
http://www.example.com/frame2.php?view=545903&epi=54683
This should work:
$epilink = "<a target='_blank' rel='nofollow' href=\"http://www.example.com/frame2.php?view=545903&epi=54683\">";
preg_match("/<a target='_blank' rel='nofollow' href=\"(.*?)\">/i", $epilink, $epiurl);
print_r($epiurl);
you can also use preg_match_all

Categories