Domanda

I have these 3 strings I need to convert to php so I can echo them. The strings are a little complex for me to fully write out in straight php so I was wondering if someone could give me an example of how to do the below.

You don't have to use my code if you don't want to, just an example on how I can echo out these lines with the html class, style and embedded PHP would be sufficient.

This is the 1st one.

Original:

<div class="newsdate" style="margin: 10px 0 !important;"><?= date("F d, Y", strtotime($r['date'])); ?></div>

This is what I tried but getting confused in the syntax:

echo "<div class='newsdate' style='margin: 10px 0 !important;'>"."date('F d, Y', strtotime($r[date])).'</div>";

2nd one (little more complex for me)

Original:

<div class="articletext"><style>.articletext img{width:100%; height:auto;}</style><?php $string = $r[3];
$max = 300; // or 200, or whatever
if(strlen($string) > $max) {
  // find the last space < $max:
  $shorter = substr($string, 0, $max+1);
  $string = substr($string, 0, strrpos($shorter, ' ')).'...';
}
echo $string;
?></div>

What I have tried:

echo "<div class='articletext'>" . "<style>.articletext img{width:100%; height:auto;}</style>';" . "
        $string = $r[3];" . "
        $max = 300;" . "
        if(strlen($string) > $max) {
            < $max:
            $shorter = substr($string, 0, $max+1);" . "
            $string = substr($string, 0, strrpos($shorter, ' ')).'...';" . "
        }
        $string.'</div>";

3rd one.

Original

<div class="btn small green white-tx"><a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/htp-news.php?id=<?=$r[0] ?>">Continue Reading</a></div>

What I have tried:

echo "<div class='btn small green white-tx'>"."
                    <a href='http://'.$_SERVER['SERVER_NAME'].'/htp-news.php?id='.$r[0].''>"."Continue Reading</a>
                </div>"

I appreciate any help.

È stato utile?

Soluzione 2

For 1st one try with :

echo "<div class='newsdate' style='margin: 10px 0 !important;'>".date('F d, Y', strtotime($r[date]))."</div>";

For 2nd try with this:

echo "<div class='articletext'> <style>.articletext img{width:100%; height:auto;}</style>";
        $string = $r[3];
        $max = 300;
        if(strlen($string) > $max) {

            $shorter = substr($string, 0, $max+1);
            $string = substr($string, 0, strrpos($shorter, ' '))."...";
        }
        echo $string."</div>";

and for 3rd try:

echo "<div class='btn small green white-tx'>
                    <a href='http://".$_SERVER['SERVER_NAME']."/htp-news.php?id=".$r[0]."'>Continue Reading</a>
                </div>";

Altri suggerimenti

I strongly suggest you don't do any of these things. Instead, keep the static HTML where it belongs, outside the PHP context.

From the example in your comments, try something like this...

<?php foreach ($rows as $r) : ?>
    <!-- first example -->
    <div class="newsdate" style="margin: 10px 0 !important;">
        <?= date("F d, Y", strtotime($r['date'])) ?>
    </div>

    <!-- second example, removed <style> as it does not belong here -->
    <div class="articletext">
        <?php
        $string = $r[3];
        $max = 300; // or 200, or whatever
        if(strlen($string) > $max) {
            // find the last space < $max:
            $shorter = substr($string, 0, $max+1);
            $string = substr($string, 0, strrpos($shorter, ' ')).'...';
        }
        echo htmlspecialchars($string);
        ?>
    </div>

    <!-- third example -->
    <div class="btn small green white-tx">
        <a href="http://<?= $_SERVER['SERVER_NAME'] ?>/htp-news.php?id=<?= $r[0] ?>">Continue Reading</a>
    </div>
<?php endforeach ?>

1st:

echo "<div class='newsdate' style='margin: 10px 0 !important;'>".date('F d, Y', strtotime($r[date])).'</div>";

3rd:

echo '<div class="btn small green white-tx"><a href="http://'.$_SERVER['SERVER_NAME'].'/htp-news.php?id='.$r[0].'">"."Continue Reading</a></div>"

First one:

echo "<div class='newsdate' style='margin: 10px 0 !important;'>".date('F d, Y', strtotime($r[date]))."</div>";

Second one: - Can you please specify what you want?

Third one:

echo "<div class='btn small green white-tx'><a href='http://".$_SERVER["SERVER_NAME"]."/htp-news.php?id=".$r[0]."'>Continue Reading</a></div>";
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top