In WordPress we use the code <?php the_title(); ?> for displaying the title of a post. Now, I want to display only the first letter of the title in a different place. How can I do that?
I have tried this, but it doesn't work:
<?php $my_title = the_title(); ?>
<?php
$first_char = $my_title[0];
echo $first_char;
?>
// Get the first character.
// $firstCharacter = $string[0];
$my_title = get_the_title();
// Get the first character using substr.
$firstCharacter = substr($my_title, 0, 1);
echo $firstCharacter;
The the_title() function will print it by default if the echo parameter is not set to false. get_the_title() will retrieve the title.
You can simply do this:
<?php echo get_the_title()[0]; ?>
As long as the title is not empty. Or:
<?php echo substr(get_the_title(),0,1); ?>
<?php echo preg_replace('/^(\w).+/','\1',get_the_title()); ?>
<?php echo str_split(get_the_title())[0]; ?>
<?php printf("%.1s", get_the_title()); ?> //echo sprintf
etc...
Or if you want to get complicated, you can use a "stream" yea!:
$f = fopen('php://memory', 'w+');
fputs($f, get_the_title());
rewind($f);
echo fgetc($f);
fclose($f);
LOL - that was the hardest way I could think of, that does what it's supposed to and doesn't have any unnecessary steps (well, except fclose, but in this case we can recover the memory);
Sandbox
Related
email: emDSDla.ccount#gmail.com
<?php
$useruser = $customer->email;
?>
<?php echo $useruser ?>
I need to create from customer email, I need to create username.
With rule:
get only first 8 characters
remove special characters # .
change to small words
result:
emdsdlac
Can anyone help me implement this to code?
update:
<?php $useruserBIG = 'emDSDla.ccount#gmail.com' ?>
<?php $resultsmall = strtolower($useruserBIG); ?>
<?php echo $resultsmall ?>
<?php
$newstr = preg_replace('/[^a-zA-Z0-9\']/', '_', $resultsmall);
$newstr = str_replace("#", '', $resultsmall);
?>
<?php echo $newstr ?>
Currently I get result:
emdsdla.ccountgmail.com
Can anybody help me improve this code (merge in one and correct with truncate) to get
result:
emdsdlac
Thanks!
update3:
currently I get correct result with this:
<?php $useruserBIG = 'emDSDla.ccount#gmail.com' ?>
<?php $resultsmall = strtolower($useruserBIG); ?>
<?php
$newstr = preg_replace('/[^a-zA-Z0-9\']/', '', $resultsmall);
?>
<?php echo substr($newstr, 0, 8) ?>
result from above code:
emdsdlac
The problem is, I cannot display in my script result some like this:
<?php echo substr($newstr, 0, 8) ?>
I can display result only with variable some like:
<?php $resultaftertruncate ?>
can anyone else help me to customize this code?
i want to use a variable content (value) as a part of a variable name before the output
this is my code in other file:
<?php $vidooid1="fk16zv6mw2jj"; ?>
<?php $vidooid2="eztx3n90w8xs"; ?>
<?php $vidooid3="eztx3n90w8xs"; ?>
<?php $vidooid4="eztx3n90w8xs"; ?>
<?php $vidooid5="eztx3n90w8xs"; ?>
<?php $vidooid6="eztx3n90w8xs"; ?>
<?php $vidooid7="eztx3n90w8xs"; ?>
<?php $vidooid8="eztx3n90w8xs"; ?>
<?php $vidooid9="eztx3n90w8xs"; ?>
<?php $vidooid10="eztx3n90w8xs"; ?>
and this is the code in the actual file:
<?php $titlenumber="1"; ?>
<?php $vidoo="https://vidoo.tv/e/"; ?>
and this is the echo final result:
<?php echo $vidoo; ?><?php echo $vidooid; ?>
I want that when I change the value that is inside the $titlenumber then that it is also changed in number that is next to the $videoid <----just here (i want to rename the string by using titlenumber)
This would be far better implemented using arrays.
e.g.
<?php
$vidoos = array(
"fk16zv6mw2jj",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs",
"eztx3n90w8xs"
);
$titlenumber = 0;
$vidoo="https://vidoo.tv/e/";
echo $vidoo.$vidoos[$titlenumber];
$titlenumber = 3;
echo "<br/>"; //line break, just for demo
echo $vidoo.$vidoos[$titlenumber];
?>
Note that arrays start their indexes at 0 by default, so if you can start titlenumber at 0 too, you can do it simply like I've shown. If not, shift the array as shown in this answer
BTW you don't have to write a new <?php on every line. Just write it once at the start of the section of PHP, and close it once with ?> at the end. Lines in between only need to end with ;.
You can use concatenation as the following example shows:
$iterator = 1;
$variableName = 'vidooid';
$vidooid1 = 'some value';
echo ${$variableName . $iterator}; // outputs 'some value'
As the others said before, you should learn how to use arrays with PHP. The above shown solution is okay, but should not be used when you can use arrays instead.
I have a variable given to me by my cms
I want to add one space before that but have tried many things lol and all dont work, below is what I have tried so far.
<?php echo str_repeat('', 1) htmlencode('$postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode(' $postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode('. $postcode_coveringsRecord['postcode'].) ?>
<?php echo ' ''htmlencode('$postcode_coveringsRecord['postcode'])' ?>
<?php echo htmlencode(' ''$postcode_coveringsRecord['postcode']) ?>
How can I minipulate
where as the variable gives me one blank space prior to the variable content.
cheers for any input
emma
These ones should work
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);
or
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);
I am trying to use substr to remove 4 characters from a URL string. The goal is to remove .jpg from the string and replace it with "-220x124.jpg".
I am using a wordpress plugin, advanced custom fields, but that is not the issue here. The issue is that the subst is not working with the Advanced custom fields code, the_sub_field. It returns the entire URL string without the last 4 characters removed. Any idea why?
Code below:
<?php if(get_field('still_uploads')): ?>
<?php $i = 0; ?>
<?php while(the_repeater_field('still_uploads') && $i <= 0 ): ?>
<?php
$imagejesse = the_sub_field('still_image');
$imagejessenew = substr($imagejesse,0,-4);
?>
<?php echo $imagejessenew.'-220x124.jpg'; ?>
<?php $i++ ?>
<?php endwhile; ?>
<?php endif; ?>
You can see an example here: http://gicreative-dev.com/blog/genre/gay/
Use strtr() function like that:
$imagejessenew = strtr($imagejesse, array(
'.jpg' => '-220x124.jpg',
));
See this for a proof: http://ideone.com/B8ZQe
Try this:
<?php
$imagejessenew = substr(trim(the_sub_field('still_image')),0,-4);
if($imagejessenew !== FALSE)
{
$imagejessenew .= '-220x124.jpg';
}
else
{
// Shit happened, the_sub_field('still_image') was shorter than 4 characters
}
?>
$imagejessenew = preg_replace('/(.*)(.(jpg|png|gif))$/i', '$1-220x124.$3', trim($imagejesse));
In my Wordpress site, I am trying to retrieve the most recent 10 posts and store them in a string. After that I will write this content into a text file. Below is the code I am using.
<?php $str = ''; ?>
<?php
require_once('../wp-blog-header.php');
query_posts('&showposts=10&order=DESC&caller_get_posts=1');
while (have_posts()) : the_post(); ?>
<?php $str .= '' .the_title() . ''; ?>
<?php endwhile; ?>
<?php $fp = fopen("latestposts.txt", "w");
fwrite($fp, $str);
fclose($fp);?>
The problem is, when I execute this page, the permalink and title are returning in this page and empty ''....'' tags are coming in text file. If I am not using the string, the href tags are returning correctly in the same file.
the_permalink() and the_title() does not return anything they are to print values.
You have to use their get_ version. Those are get_permalink() and get_the_title()
<?php $str .= '' .get_the_title() . ''; ?>
This is more of a wordpress question, but you should be using get_permalink() and get_the_title() instead of the functions you have there. Those functions will echo the link and title, and not return it in string form for use in your concatenation.