I have problem with comma. I have a code
<?php echo get_the_subtitle($post); ?>, <?php echo get_the_date(); ?>
and i need show comma ',' only when
<?php echo get_the_subtitle($post); ?>
have a subtiitle.
Example: subtitle, date. If no have a subtitle not show coma. I do something like this <?php echo get_the_subtitle($post, ','); ?> but comma is before subtitle not aftre ;/
The documentation states that the second parameter of this function is the text before and the third parameter is the text after the subtitle.
So you should do something like this:
<?php echo get_the_subtitle($post, '', ','); ?>
<?php
$subtitle = get_the_subtitle($post);
echo $subtitle ? $subtitle . ', ' : '';
echo get_the_date();
?>
You need check get_the_subtitle($post) function returns something or not. If it returns something you should echo it with comma or it does not return something you should echo only date.
You could do this:
echo (get_the_subtitle($post) != '') ? get_the_subtitle($post) .','. get_the_date() : get_the_date();
If the subtitle isn't blank, echo the subtitle and the date (with the comma), else just echo the date.
Related
I have this html
<span class="item-title">Title</span>
<span class="item-cat">sub-text</span>
I have in database string like Title sub-text. How can I place sub-text in bottom <span> when str_replace match space? I've tried something like this
<span class="item-title">'.str_replace(' ',"<span class="item-cat">sub-text</span>",$row['category']).'</span>
But seems not right because I don't know how to divide the string and show only second part in second div. Any help is appreciated.
In case of sub-text won't have any spaces you can simply use explode() function create two strings. One for title and another one for sub-text.
Code would look seomthing like this,
<?php
$mainTitle="Title sub-text";
$parts=explode(" ",$mainTitle);
?>
<span class="item-title"><?php echo $parts[0]; ?></span>
<span class="item-cat"><?php echo $parts[1]; ?></span>
<?php // Other code ?>
explode() just split the string by space( in this case) which will create array of two elements, of which first one is title and second
one would be sub-text.
To split this you can use this
<?php
$str = 'Title sub-text';
$myarray = str_word_count ($str,1);
// echo $myarray[0]; // Title
//echo $myarray[1]; // sub-text
?>
<span class="item-title"><?php echo $myarray[0]; ?></span>
<span class="item-cat"><?php echo $myarray[1]; ?></span>
For more info about str_word_count please read http://php.net/manual/en/function.str-word-count.php
Note: You can also use explode.
I want to show this sentence:
?>
<h2>Date <?php echo "$day"."/"."$month"."/"."$year"."<br>"; ?></h2>
<?php
I have tried:
echo '<h2>Date '$day'.'/'.'$month'.'/'.'$year'.'<br>' </h2>';
echo '<h2>Date <?php echo \"$day/\".\"/\".\"$month\".\"/\".\"$year\".\"<br>\"; ?></h2>';
Without success.
If a line contains both HTML and PHP code, what is the proper format to display it?
You have a "dot mess".
You can concatinate PHP variables and strings like
<?pho echo 'string' . $var . 'string' . $var1 . $var2; // and so on ?>
So correct solutions would be
<?php
echo '<h2>Date ' . $day . '/'. $month .'/' . $year .'<br></h2>';
echo '<h2>Date ' , $day , '/', $month ,'/', $year ,'<br></h2>';
echo "<h2>Date $day/$month/$year<br></h2>";
echo "<h2>Date {$day}/{$month}/{$year}<br></h2>";
?>
You can even use commas , for echoing strings and variables that's a bit faster. Cause PHP does not concatinate all parts together to a single string and echos it but echos every part separatly (so less operations are needed).
Note that if you put a string in double quotes " you can use $var or {$var} inside the string cause PHP is looking vor PHP variables inside a double quoted string and replaces them by their containing value.
But the fastest solution is the , separated version.
or
As lonesomeday mentioned there is also a C like solution possible in PHP
<?php printf('<h2>Date %s/%s/%s<br></h2>', $day, $month, $year); ?>
or (but not recommended by me)
The complicated and none readable version
<h2>Date <?php echo $day; ?> / <?php echo $month; ?> / <?php echo $year; ?><br></h2>
And the shorted version of the above code (just for completeness, short tags have to be enabled in PHP versions lower than 5.4 to work)
<h2>Date <?= $day; ?> / <?= $month; ?> / <?= $year; ?><br></h2>
For even more information about string concatination read the offical PHP doc site about String Operations.
The nicest way to do this is probably with printf, which substitutes variables into your string according to placeholders:
printf('<h2>Date %s/%s/%s<br></h2>', $day, $month, $year);
?>
<h2>Date <?php echo $day."/".$month."/".$year."<br>"; ?></h2>
<?php
You don't need quotes when using variables.
Try
echo '<h2>Date ' . $day . '/'. $month .'/' . $year .'</h2>';
Try this...
This will display the code as it is in your browser
<?php
echo htmlspecialchars('<h2>Date <?php echo \"$day/\".\"/\".\"$month\".\"/\".\"$year\".\"<br>\"; ?></h2>');
?>
O/P:
<h2>Date <?php echo \"$day/\".\"/\".\"$month\".\"/\".\"$year\".\"<br>\"; ?></h2>
Can you try this,
echo "<h2>Date $day / $month / $year <br></h2>";
I think this is your answer
<h2>Date <?php echo "\$day"."/"."\$month"."/"."\$year"."<br>"; ?></h2>
or
<?php echo '<h2>Date '.$day.'/'.$month.'/'.$year.'<br></h2>'; ?>
try this
echo '<h2>Date '$day'.'/'.'$month'.'/'.'$year'.'<br>' </h2>';
echo '<h2>Date <?php echo \"$day\"."/".\"$month\"."/".\"$year\".\"<br>\"; ?></h2>';
Im currently trying to make the first word of any/all my Joomla Articles/Categories/Blog headings have one color and then the rest of the sentence be the site default. I have found the code below that does change the color but it only works if the heading contains 2 words and if there are more then it removes all formatting.
<?php if ($this->params->get('show_page_heading')) : ?>
<?php
$title = $this->escape($this->params->get('page_heading'));
$title = str_replace(' ', '<span>', $title);
echo "<h1>" . $title . "</h1>";
?>
<?php endif; ?>
Thanks!
In this scenario, you must account for the heading being either 1 word or multiple words.
Try this..
// check to see if there are multiple words by the count of the space character
if(substr_count($title,' ') > 0) {
// multiple words
// replace the FIRST space with closing span tag
$title = '<span>'.preg_replace('/\ /', '</span> ', $title, 1);
}
else {
// one word, just close the span
$title = '<span>'.$title.'</span>';
}
echo "<h1>" . $title . "</h1>";
Be mindful that if the first character in the heading is a space, then you will get an empty span and not the desired effect.
I am trying to output the results from a field that is read from mysql.
the field has this in it.
< ? echo "yes"; ? >
however when i try to print the field... it is null.
do i have to escape it on output?
i dont want to execute the code.. i want to output what is in the field as text
You have to escape with htmlspecialchars()
$val = htmlspecialchars ('<?php echo "yes"; ?>');
echo $val;
I guess htmlspecialchars is that what you want:
<?php
$str = "<?php echo 'lol'; ?>";
echo htmlspecialchars($str);
?>
it outputs <?php echo 'lol'; ?>
I was wondering how can I print out whitespace in the following code below.
<?php echo "$first_name", "$last_name" ; ?>
Just, well, add whitespace to your output?
<?php echo "$first_name $last_name with some whitespace" ; ?>
Sometimes, it the obvious answer is right - even in PHP!
If you want to concatenate several strings without the double quotes (e.g. when using single quotes):
$string = $first_name . ' ' . $last_name; // The dot is a concatenation operator
<?php
echo "$first_name $last_name";
?>
Notice that when you use ' any var aren't display, with " does.
So, good is:
<?php echo "$first_name $last_name" ; ?>
bad
<?php echo '$first_name $last_name' ; ?>
If you want to keep using commas:
<?php echo $first_name, ' ', $last_name; ?>