if less than x don't show a "..."
<?php echo substr(stripslashes($row['news_title']), 0, 20). ".."; ?>
I have it to show more than x if more than 20, but it shows "..." when there's 10 chars. Is there anyway I could have it not to show?
any tutorials?
Thanks!
Try like
<?php echo substr(stripslashes($row['news_title']), 0, 20);
if(strlen($row['news_title']) > 20)
echo "..";
?>
You could use CSS tricks, but this would be the code for doing it server-side:
if (strlen($row['news_title']) <= 20) {
echo htmlspecialchars($row['news_title']);
} else {
echo htmlspecialchars(substr($row['news_title'], 0, 20)), '...';
}
Note that strlen() counts bytes and not characters per se; this is important when you start working with Unicode, in which case you may want to consider using mb_strlen().
Btw, using stripslashes() is somewhat of a red flag; if your quotes come out as escaped, the problem lies somewhere else and shouldn't be a problem of the presentation layer ... in fact, you should be using htmlspecialchars() instead.
This would do.
<?php echo strlen(stripslashes($row['news_title']))>20 ?substr(stripslashes($row['news_title']), 0, 20)."...":stripslashes($row['news_title']); ?>
Try to write your own sub string function:
it can be somthing similar like this
http://www.sranko.com/nwP3LFit
Related
I'm trying to create in PHP a randomized string script, but complex, because each line must have a different color, the color is not important, it can be random, but should not be the same twice.
The output must be something like this :
Hello#in blue color
Hello#in red color
Hello#in pink color
i've tried with the str_repeat function,
echo "Colored random String Generator";
echo "<br>";
echo str_repeat("Hello <br>", 100);
but there are too few parameters, i can't really put a <span style="color:$randomColor> between them
Did i miss something ?
You may need to tweak the random color generator as I chucked it together but this is basically what you are after I believe:
function randomColor() {
return str_pad(dechex(rand(0x000000, 0xFFFFFF)), 6, 0, STR_PAD_LEFT);
}
echo "Colored random String Generator<br>";
$repeatCount = 100;
for ($x = 0; $x <= $repeatCount; $x++) {
echo "<span style=\"color: #".randomColor().";\">Hello</span> <br>";
}
Try like this :
echo str_repeat("<span style="color:".$randomColor.">Hello ", 100);
Mark it as solved if this helps or post your entire code in the comments.
If I have, say, 8.1 saved as a string/plaintext, how can I change that into the integer (that I can do addition with) 81? (I've got to remove the period and change it into an integer. I can't seem to figure it out even though I know it should be simple. Everything I try simply outputs 1.)
You can also try this
$str = '8.1';
$int = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
echo $int; // 81
echo $int+1; // 82
DEMO.
If you're dealing with whole numbers (as you said), you could use the intval function that is built into PHP.
http://php.net/manual/en/function.intval.php
So basically, once you have your string parsed and setup as a whole number you can do something like:
intval("81");
And get back the integer 81.
Example:
$strNum = "81";
$intNum = intval($strNum);
echo $intNum;
// "81"
echo getType($intNum);
// "integer"
Since php does auto-casting, this should work:
<?php
$str="8432.145522";
$val = str_replace('.','', $str);
print $str." : ".$val;
?>
Output:
8432.145522 : 8432145522
Not sure if this will work. But if you always have something.something,(like 1.1 or 4.2), you can multiply by 10 and do intval('string here'). But if you have something.somethingsomething or with more somethings(like 1.42 and 5.234267, etc.), I don't know what to say. Maybe a function to keep multiplying by ten until it's an integer with is_int()?
Sources:
http://php.net/manual/en/function.intval.php
http://php.net/manual/en/function.is-int.php
Convert a string to a double - is this possible?
I write code that work only with english letters. when I ctry to do it with hebrew letter I get error.
the code:
<?php
$idan="emr";
$firstletter = mb_substr($idan, 0, 1, 'UTF-8');
if ($firstletter=='e')
echo "tov";
else echo "lo tove";
?>
work good.
but the code:
<?php
$idan="אבהבה";
$firstletter = mb_substr($idan, 0, 1, 'UTF-8');
if ($firstletter=='א')
echo "tov";
else echo "lo tove";
?>
not work, How I can to fix it?
You do realize that mb_substr works from left to right? (is Hebrew written from right to left?) You are therefore reading the first character which is ה not א. To get the first letter from the right (the last one), use -1 as a starting index:
$firstletter = mb_substr($idan, -1, 1, 'UTF-8');
You can also use mb_internal_encoding("UTF-8"); to set the encoding for every call, instead of propagating it (if you're making many calls to mb_ functions)
Edit: Following your comments, here's a quick example of a script that handles letters depending on the language:
$desiredLetter = 'e';
$startIndex = 0;
// some condition to figure out the language
if(mb_detect_encoding($idan, 'ASCII', true) == FALSE)
{
$desiredLetter = 'א';
$startIndex = -1;
}
$firstletter = mb_substr($idan, $startIndex, 1, 'UTF-8');
if ($firstletter == $desiredLetter)
echo "tov";
else
echo "lo tove";
Your code worked correctly (e.g. printed tov) when I saved it to a PHP file and ran it. If you are seeing different results, you may want to check your text editor settings and ensure that you're saving the file as UTF-8.
OK so I have an Item description being echoed out from a database,
What I want to do is say:
if the description is over 100 characters display the 100 then display a hyper-link to read more about it.
else display the description.
I have already used strlen to get the length of the string but how do I tell it to only display that 100 then the hyperlink? HELP :( :(
Use something like this:
if(strlen($string) > 100) {
echo substr($string, 0, 100).' '.$hyperlink;
}
else {
echo $string;
}
Use substr():
if (strlen($str) > 100) {
$str = substr($str,0,100);
}
How's this?
if (strlen($theString) > 100)
{
echo (substr($theString, 0, 100));
writeHyperLink($theString);
}
else
{
echo $theString;
}
Have a look at the function substr, and possibly wordwrap.
If you can live with an additional database access you can simply store the row id (embed it in the hyperlink as a GET parameter etc.) and pass it as a parameter to the page showing full messages.
I'm trying to make a function that will take short hand hex color to the long hex color.
For example if someone submits "f60" it will convert to "ff6600". I understand I need to repeat each number as itself, but what is the most efficient way to do this?
Thank you.
This should work. However, you'll want to make sure the strings aren't prepended with a # due to the exact strlen comparison.
// Done backwards to avoid destructive overwriting
// Example: f60 -> ff6600
if (strlen($color) == 3) {
$color[5] = $color[2]; // f60##0
$color[4] = $color[2]; // f60#00
$color[3] = $color[1]; // f60600
$color[2] = $color[1]; // f66600
$color[1] = $color[0]; // ff6600
}
$fullColor = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2];
You can access characters of a string as an array.
this question cannot miss the good old regexes :D
$color = preg_replace('/#([\da-f])([\da-f])([\da-f])/i', '#\1\1\2\2\3\3', $color);
not the best solution though …
Not the most efficient, but an alternative
with these you can duplicate every kind of string with every length not only 3 as Hex colors
<?php
$double='';
for($i=0;$i<strlen($str);$i++){
$double.=$str[$i].$str[$i];
}
?>
or
<?php
$str="FCD";
$split=str_split($str);
$str='';
foreach($split as $char) $str.=$char.$char;
echo $str;
?>
You could also use regex or other...