Determine if it's end of a string in PHP - php

Is there a way to determine if it has reached the end of a string or not.
Right now I'm using this PHP function
<?php echo substr('some text', 0, 7)?>
To limit the character number to be displayed. Now I would like to add "..." to the text but only if it's not the end of the string.
So when I have a text "Hello!" it would display the whole thing and if I have a text "Determine" it would not only display "Determi" but also adds this "..." like that "Determi...".
I've tried these ellipse and overflow ways but they didn't work for me.
Would something like this described above be possible?

You can achieve this with the built-in function mb_strimwidth:
<?php
echo mb_strimwidth("some text", 0, 7, "...")
// output: "some te..."
?>

You could do something like this :
<?php echo (strlen($text) > 7 ? (substr($text, 0, 7) . '...') : $text) ?>
If string has more than 7 characters, you echo only the first 7 ones and add ..., otherwise you just echo the text

function trimStr($str, $len = 7) {
return strlen($str) > $len ? substr($str, 0, $len) . '...' : $str;
}

Related

Change the color for first letter in a string php

I just learn PHP by myself so I have many things to ask. I read a sample code about how to change color for the first letter in the string but I don't understand what \1 in a span tag of HTML.I know it is used to change the color to red but why \1 but not another character. Really appreciate your help.
<?php
$text = 'VietJack Team';
$text = preg_replace('/(\b[a-z])/i','<span style="color:red;">\1</span>',$text);
echo $text;
?>
you don't have to use regex, and you shouldn't.
php have a native function called substr_replace, and it gets the job done.
example :
<?php
function str_replace_chr(string $str, int $position, string $format): string
{
$chr = $str[$position - 1];
$p1 = $position - 1;
$p2 = $position - strlen($str);
$replace = sprintf($format,$chr);
return substr_replace($str,$replace,$p1,$p2);
}
echo str_replace_chr('VietJack Team', 1, '<span style="color: red;">%s</span>');
online eval : https://3v4l.org/hvv3p

How to add "..." to the end of product title, php echo

I'm trying to add "..." after my product title.
<a href="index.php?section=1&show=<?php echo $row['id_product']; ?>">
<?php
$title = $row['name'];
$newtext = str_pad($title, 6, "...");
echo $newtext; ?>
</a>
But php print only the $row['name'] and doesn't add "..."
How is it possible to make this happen?
From str_pad() docs
If the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place, and input will be returned.
You can get the length first of your string then just add the length of the string to pad. In your case,
$title = $row['name'];
$length = strlen($title) + 6;
$newtext = str_pad($title, $length, "...");
echo $newtext;
For cutting the string to a smaller string, then adding "...". You can use substr() then concatenate. With additional check for more than 6 string length
$title = $row['name'];
$newtext = strlen($title) > 6 ? substr($title, 0, 6) . "..." : $title;
echo $newtext;

Substr String With HTML In PHP

I have some text that comes back from my database like so:
<span rgb(61,="" 36,="" 36);="" font-family:="" 'frutiger="" neue="" w01="" book',="" 'helvetica="" neue',="" helvetica,="" arial,="" sans-serif;="" line-height:="" 23.8px;"="">The Department of ...
I use echo html_entity_decode($item->body); to display:
The Department of ...
However, if I use the PHP substr function on this content it never displays correctly. It will display the first x characters of HTML and not the HTML formatted text.
Here's what I tried: echo substr(html_entity_decode($item->body), 0, 5);
But it doesn't display anything. If I try an amount like 0, 200); it will display:
The Department of Molec
But this is most definitely not the first 200 characters of the formatted text because the first character is T.
My idea is that there must be way to format and then substr, even though I can't get it to work using html_entity_decode() and substr() by themselves.
Can anybody help me out here? Thanks!
Try to use this instead of html_entity_decode():
strip_tags($item->body);
strip_tags removes all HTML tags from the string. So you better of treating the string and then do something with it.
You will see the output in the source code, but it is not beeing rendered. The source code will show:
echo substr(html_entity_decode($item->body), 0, 5);
// Output: "<span"
What you probably want to do is search for the end of the html-tag, and display 5 characters after that, like:
$text = html_entity_decode($item->body);
$start = strpos( $text, '>' ) + 1;
echo substr( $text, $start, 5 );

Remove ellipsis (&helip;) after sub-string concatenation

I am concatenating two sub-strings with an ellipsis (...) added at the end of the first sub-string. However I want this elipsis to be removed after the concatenation.
Is this possible via a script or other means, Thanks:
<?php echo substr($post_text_result2, 0, 400) . "…";?><div id="second_post" class = "hidden"><?php echo substr($post_text_result2, 400, 5000);?></div>:
str_replace deletes the elipsis like requested, however does not fully work in my situation:
The code below works however, The first sub-string is repeated. I need a way to remove the first sub-string.
<?php
$string = substr($post_text_result2, 0, 400) . "…";
echo $string;
?>
<div id="second_post" class = "hidden">
<?php
$string= str_replace('…','',$string); echo $string;
echo $string;
echo substr($post_text_result2, 400, 5000);
?>
easy
<?php
$string = substr($post_text_result2, 0, 400) . "…";
$string = str_replace('…','',$string);
?>
If you want an ellipsis always at the end, just do this:
<?php
$string = substr($post_text_result2, 0, 400) . "…";
$string = str_replace('…','',$string) . "…";
?>
You can store the length of the first string A and then build a new string concatenating 2 substrings:
substring 1 from 0 until length(A) - 3
substring 2 from length(A) until total length
You also could use a regex and replace "..." with "", but that could remove other "..." which is maybe unwanted.
Sorry for no code, I can't PHP but you know how to do it ;)

Wordpress/PHP: Shorten title if title characters are more than 8 characters

I am trying to learn how to shorten a title only if it is over 8 characters long. If it is longer than 8 characters, then echo the first 8 characters and put an ellipse after it.
Here is how I am getting the title:
<?php echo $post->post_title ?>
Any help would be greatly appreciated. This will be a great learning lesson for me so I can replicate this in the future, so again any help would be amazing.
<?php
if (strlen($post->post_title) > 8)
echo substr($post->post_title, 0, 8) . ' ...';
else
echo $post->post_title;
?>
Alternatively, if You have the mbstring extension is enabled, there's also a shorter way as suggested by Gordon's answer. If the post's encoding is multibyte, You'd need to use mbstring anyway, otherwise characters are counted incorrectly.
echo mb_strimwidth($post->title, 0, 8, ' ...');
You can use mb_strimwidth
echo mb_strimwidth('Your Title', 0, 8, '…');
If you want to truncate with respect to word boundaries, see
Truncate a multibyte String to n chars
You should do this in plugin because if you change the theme the changes will be lost
you can try this.
$maxlength = 8;
if (strlen($post->post_title) > $maxlength)
echo substr($post->post_title, 0, $maxlength) . ' ...';
else
echo $post->post_title;
So by now you no need to change max char in all the line of code.
Thanks.

Categories