How to write ... after 5 characters [duplicate] - php

This question already has answers here:
Limiting the output of PHP's echo to 200 characters
(15 answers)
Closed 3 years ago.
I'm beginner and I have a question. I would like that if my text is more than 5 characters, that it writes 5 characters + ... . It's for shortening a name in navbar if its too long, it causes bugs in my nav.
Here's an example of desired result:
My text: TheTest
output: TheTe...
I think I could do this with strlen() but have no idea how to do this. Maybe with an overflow ?
I tried to do it with text-overflow: ellipsis; but it didn't work either
Can you help me please ?

just check the length to see if its more than 5 character or not
$mesage="This is text String";
if(strlen($mesage)>5){
echo substr($mesage,0,5)."...";
}
else{
echo $mesage;
}
More infos about strlen(): http://www.php.net/manual/de/function.strlen.php
substr(): https://www.php.net/manual/en/function.substr.php

Related

Find string, then find string in-between chars [duplicate]

This question already has answers here:
What kind of data format is that?
(2 answers)
Closed 4 years ago.
I'm having a hard time with this....
So, I have a load of text:
a:13:{s:9:"live_odds";i:0;s:14:"show_matchbook";i:0;s:12:"show_betfred";i:1;s:16:"show_williamhill";i:1;s:12:"betfair_show";i:1;s:16:"betfair_username";N;s:16:"betfair_password";s:9:"";s:20:"betfair_affiliate_id";s:3:"888";
Now, what I am trying to do is search for betfair_affiliate_id";s:3: within that bulk of text and then display the text between the speech marks.
I was trying
$betfred_show = 'betfair_affiliate_id";s:3:"';
$betfred_show_value = substr($string, strpos($string, $betfred_show) + strlen($betfred_show), 3);
which does work, it brings back 888... but it's not really future proof as this just gets the 3 next digits. However, these could change to 4 or 5 digits.
Any suggestions?
It is a serialised array. You can simply unserialize it and access the key
$array = unserialize($input);
$output = $array['betfair_affiliate_id'];

REGEX with PHP (what am I doing wrong?) [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
Let's say I want to place the bold content into a variable called $question_text.
1} What color is the sky:
A) Answer A
B) Answer B
C) Answer C
Answer: A
How do I use preg_match to do that?
I tried:
if(preg_match("#}(.*)#", $question, $question_match))
{
//Extract the question text
$question_text = trim($question_match[1]);
But it only gives me the first line: **What color is the sky: **
What am I doing wrong?
#(?s)}(.*)+?Answer: [A-Z]#
Worked for me!

PHP return 1 number after dot in float [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 6 years ago.
I have got this php code
$Likes=1112;
$Likes=$Likes/1000;
echo $Likes."k";
This code returns me 1.112k,but my goal is to get 1.1k
Use the number_format function:
echo number_format($Likes,1)."k";
And a coding style advice: don't start your variables with an upper case letter!

String length for various encoding [duplicate]

This question already has answers here:
Php length of cyrillic string doubles its value
(3 answers)
Closed 7 years ago.
I'm doing application for string length check. When checking Cyrillic string there shows wrong because of the unicode. How to solve this problem?
$str=strlen ('abc');
echo $str; // result is 3
$str=strlen ('АБС');
echo $str; // result is 6. How to find correct value
Was little curious about this question.
First options is as suggested by Rizier123(no doubt, that works perfectly.)
Second:
You can also use utf8_decode() to get the result.
<?php
$str=strlen (utf8_decode('АБС'));
echo $str;
?>
Check demo here

How to find 3 or more of a character in a string? [duplicate]

This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Closed 9 years ago.
$string = "oidjdssd , odi,jdois, 3089u,, oisdjsd";
How do i find out if theres more than 3 commas in the string above in the best way?
I would suggest substr_count. You can see if the result is >3 to see if there's more than three.
echo count_chars($string)[ord(',')];
Or for PHP<5.4
$chars = count_chars($string);
echo $chars[ord(',')];
BTW: As it seems, that you are handling CSV-data, you should have a look at str_getcsv()

Categories