Hey guys.
Unfortunately I can't break out of an echo to run the if statement I want to run because it's pulling data for a certain row.
I'd like text truncated to a certain amount of characters or a certain width but if the textual content is less than that amount of characters I want it untouched.
My code so far is... -
echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', substr($row[album], 0, 30))."...</a></td></tr>";
-
And it generates this output (the right hand table of newest items in the album bit): <Link removed>
I'm a bit of a novice, can anyone offer any suggestions?
Much appreciated
5 3906 Atli 5,058
Expert 4TB
Hey.
Have you tried doing something like this? -
$text = ''; // Something from your database
-
$maxChars = 78; // Or whatever.
-
-
if(strlen($text) > $maxChars) {
-
$text = substr($text, 0, $maxChars - 3);
-
$text .= "...";
-
}
-
-
echo "Something with the '$text' in it.";
Hey,
I tried it but it hasn't worked. I don't know if I'm doing it right: - $text = '$row[album]'; // Something from your database
-
$maxChars = 10; // Or whatever.
-
-
if(strlen($text) > $maxChars) {
-
$text = substr($row[album], 0, $maxChars - 3);
-
$text .= "...";
-
}
-
-
-
-
echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".$row[album]."</a></td></tr>";
I have to use .$row[album]. in the echo bit to pull the album for that certain row.
Any ideas? it's not truncating at all.
Atli 5,058
Expert 4TB
The `$row['album']` variable doesn't "pull" data for that row. It is the data for that row. The data has already been pulled from the result set into the $row array, which you can use in whatever way you want.
So you can edit the `$row['album']` if you want, or use that to create a replacement variable to use in your echo statement.
You can use either of these methods, or mix them together in whatever way you want. You just need to make sure to echo the variable that is set to the return value of the `substr()` function, because that is what is actually truncating the string. - <?php
-
$maxChars = 10;
-
if(strlen($$row['album']) > $maxChars) {
-
$row['album'] = substr($row['album'], 0, $maxChars - 3);
-
$row['album'] .= "...";
-
}
-
echo "...{$row['album']}...";
-
?>
- <?php
-
$maxChars = 10;
-
$substitute = $row['album'];
-
if(strlen($substitute) > $maxChars) {
-
$substitute = substr($substitute, 0, $maxChars - 3);
-
$substitute .= "...";
-
}
-
echo "...{$substitute}...";
-
?>
Thanks, this worked lovely!
You could also make use of the ternary operator. It's like a function that returns one thing if the condition is true, or another thing if it is false. - echo "..." . ($condition ? "true" : "false") . "...";
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ciprian Ilie |
last post by:
Hi there,
If you look at the code below, you will see that I am using a template
in order to display some photos on my website.
I also have "previous" and "next" buttons/link which increment the...
|
by: Joe Blow |
last post by:
I'm sure I'm missing something blindingly obvious here. I am relatively new
to PHP, so excuse dumb mistakes.
I a trying to check a form submitted from an earlier page, and check $_POST
for empty...
|
by: dmbkiwi |
last post by:
I am new to this group, and relatively new to python programming, however,
have encountered a problem I just cannot solve through reading the
documentation, and searching this group on google.
I...
|
by: Invalid User |
last post by:
While trying to print a none empty list, I accidentaly put an "else"
statement with a "for" instead of "if". Here is what I had:
if ( len(mylist)> 0) :
for x,y in mylist:
print x,y
else:...
|
by: Ron Adam |
last post by:
There seems to be a fair amount of discussion concerning flow control
enhancements lately. with, do and dowhile, case, etc... So here's my
flow control suggestion. ;-)
It occurred to me (a...
|
by: Jim Michaels |
last post by:
C:\prj\quiz\withusers>php tareports.php
PHP Parse error: syntax error, unexpected T_ELSE in
C:\prj\quiz\withusers\tareports.php on line 205
this is the section of code.
if (isset($row4)) {...
|
by: naughtybynature |
last post by:
<html>
<head>
<title>Search Questions</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$query = '';
|
by: naughtybynature |
last post by:
<html>
<head>
<title>Search Questions</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$query = '';
|
by: pradeep.thekkottil |
last post by:
I'm setting up an auction website using PHP and MySQL. There in the
section where logged in members can put up new auction in a form, I
want to run a form validation where I used if else statements...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |