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.
Related
Im trying to add a PHP variable to a href url
This is the code:
PHP
$uid= $_SESSION['userid'];
HTML
<a href=http://example.com/uid= <?php echo ".$uid."?> /a>
How do you add, when I do it, this is what it redirect too: http://example.com/uid=.
Try this,
<?php
#session_start();
$uid= $_SESSION['userid'];
?>
<a href="http://example.com/?uid=<?php echo $uid; ?>" >Your link text</a>
echo "link description";
Perhaps something like this:
print '<a href=http://example.com/?uid=' . $uid . '>Link</a>';
try this
<?php
$url = 'https://www.w3schools.com/php/';
?>
PHP 5 Tutorial
Or for PHP 5.4+ (<?= is the PHP short echo tag):
PHP 5 Tutorial
or
echo 'PHP 5 Tutorial';
This is one of the most helpful
echo "<td>".($tripId != null ? "<a target=\"_blank\"href=\"http://www.rooms.com/r/trip/".$tripId."\">".$tripId."</a>" : "-")."</td>";
It will work!
This will work
<a href=http://example.com/?uid= <?php echo $uid ?> Myurl</a>
Try this:
<a href=http://example.com/uid= <?=$uid?> /a>
You're doing a couple things wrong.
In HTML you should quote attribute values such as href
In PHP you're not concatenating anything, just echoing
You forgot the link text
For readability, you can (probably) use the <?= shorthand instead of <?php echo
<a /a> is broken tag syntax. Should be <a>...</a>
End result is this:
Link Text
I am currently trying to create a shopping cart for my website and I have images of products stored in a database and I want to include them within <img src> . By putting $get_row[imagesrc] within the src. I need to know the correct way to add it to the below code as I dont fully understand the ' and . tags
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['imagesrc'].
'<br/>£'.number_format($get_row['price'],2).'Add</p>';
This should achieve what you're looking for:
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'" /><br/>£'.number_format($get_row['price'],2).'Add</p>';
The ' character defines a string literal when it is wrapped around a series of characters.
The . character is used for concatenating strings for output or storage.
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'"><br/>£'.number_format($get_row['price'],2).'Add</p>';
. concatenates two strings, and ' is wrapped around a string.
so
echo 'Hello '.'World'; // Shows Hello World
I'd split yours up to make it easier to read:
echo '<p>';
echo $get_row['name'].'<br/>';
echo $get_row['description'].'<br/>';
echo '<img src="'.$get_row['imagesrc'].'" /><br/>';
echo '£'.number_format($get_row['price'],2);
echo 'Add';
echo '</p>';
But it all looks OK.
echo '<p>'.$get_row['name'].'<br/>
<img src="'.$get_row['imagesrc'].'" alt="'.$get_row['name'].'"><br/>
<br/>£'.number_format($get_row['price'],2).'
Add</p>';`
echo '<img src="'.$get_row['imagesrc'].'">';
Try that.
A specific answer has been given:
echo '<img src="'.$get_row['imagesrc'].'">';
Nonetheless, it's worth adding that you should:
You should escape output - with htmlspecialchars() or otherwise.
echo '<img src="' . htmlspecialchars($get_row['imagesrc']) . '">';
Read the documentation on PHP Strings.
Check out this way of including PHP in your HTML. It's much easier to read and maintain. The last line in the paragraph is your image tag.
<p>
<?php echo $get_row['name']; ?><br/>
<?php echo $get_row['description']; ?><br/>
<?php echo $get_row['imagesrc']; ?><br/>
£<?php echo number_format($get_row['price'],2); ?>
Add
<img src="<?php echo $get_row['imagesrc']; ?>" />
</p>
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}
I have the following
<?php echo '<img src="/wp-content/themes/CAFC/images/cards/big/'.get_post_meta($post->ID, "bigcard", true).'" alt="'.the_title().'" />'; ?>
For some reason however my 'the_title' variable is appearing before my image when outputted as so...
UK Fuels Fuel Card
<img alt="" src="/wp-content/themes/CAFC/images/cards/big/ukfuels.png">
Can anybody give me an idea of where im going wrong?
This is because the_title() auto echoes the title. Try get_the_title() instead.
Try to use as:
$title = the_title();
and, use this $title in place of the_title();
Use this instead:
<?php echo '<img src="/wp-content/themes/CAFC/images/cards/big/'.get_post_meta($post->ID, "bigcard", true).'" alt="'.get_the_title().'" />'; ?>
I think it's your browser. It changes the position of attributes for you. F. ex. Firefox can fix some HTML code according to standard.
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>