Jump to content

Help with string manipulation


R0CKY

Recommended Posts

I'm trying to edit an existing php page so that it trims a string to 20 characters, but I cannot figure out how to do it (n00b).

The relevant loop is below..

[quote]
$show = "20";
$template = "<span class=\"style1\"> {number}.<a href=\"{filelink}\">{filename} </a></span><br>";
$i = 1;
while ($file = mysql_fetch_object($result)) {
$line = str_replace("{number}", $i, $template);
$line = str_replace("{filelink}", "$config[3]/pafiledb.php?action=file&id=$file->file_id", $line);
$line = str_replace("{[b]filename[/b]}", $file->file_name, $line);
if ($list == "newest") {  $infoline = str_replace("{date}", date("n/j/y", $file->file_time), $info); }
if ($list == "downloads") {$infoline = str_replace("{downloads}", $file->file_dls , $info); }
if ($list == "rating") {
$ntv = $file->file_totalvotes - 1;
$infoline = str_replace("{rating}", @round($file->file_rating/$ntv,2), $info);
$infoline = str_replace("{votes}", $ntv, $infoline);
}
$line = str_replace("{info}", $infoline, $line);
echo $line;
$i++;
}[/quote]

The part I need trimmed back to 20 chars is the filename, so that when the $line variable is output to the browser, the filename part is no longer than 20 chars - but I am not clued up on PHP so although I know I need to use SUBSTR, I do not know how to apply it in the above situation.

Any help would be much appreciated.
Link to comment
Share on other sites

Hmmm, your code has $filename, but my code does not have the $, it has {filename} - will it work the same, so my new code would change from

  $line = str_replace([b]"{filename}"[/b], $file->file_name, $line);

to
  $shortFile = substr($filename, 0, 20);
  $line = str_replace[b]($shortfile[/b], $file->file_name, $line);

I have a feeling that's not right...?
Link to comment
Share on other sites

That worked Thorpe, thank you.

What is actually happening with this piece of code?

[b]$file->file_name[/b]

Is it a new string being created, and it's value coming from a sql field?

If anyone has the inclination, what I ultimately wanted to do with the output $line, was have it check the length of {filename} and if it was over 20, then trim it to 17, and add a string of three periods "...", that would be very cool.

Thanks again.
Link to comment
Share on other sites

[quote]Is it a new string being created, and it's value coming from a sql field?[/quote]

Pretty much.

Add this to the top of your file....

[code=php:0]
function trimlen($string) {
  if (len($string) > 20) {
    $string = substr($string,0,17).'...';
  }
  return $string
}
[/code]

Then, change the last line I gave you to....

[code=php:0]
$line = str_replace("{filename}", trimlen($file->file_name), $line);[/code]
Link to comment
Share on other sites

Thank you Jesi, I feel we are getting closer, but I still get no output  :(

Here is the full page, with the added code in bold - something of which is stopping the page from executing.

[quote]<style type="text/css">
A:link {text-decoration: underline; font-weight: bold; color:#CCCCCC} A:visited {text-decoration: underline; font-weight: bold;; color: #CCCCCC} A:active {text-decoration: none;} A:hover {text-decoration: underline; color:#00FF33;
}
body {
background-color: black;
font-family: Tahoma;
font-size: 8pt;
color: 00CC00
}
</style>
<?php
[b]function trimlen($string) {
  if (len($string) > 20) {
    $string = substr($string,0,17).'...';
  }
  return $string;
}[/b]
/*
  paFileDB 3.1
  ©2001/2002 PHP Arena
  Written by Todd
  todd@phparena.net
  http://www.phparena.net
  Keep all copyright links on the script visible
  Please read the license included with this script for more information.
*/
$path = "**removed**";
//This is the full system path to your paFileDB folder. You will need to obtain this information from a phpinfo.php file, or from your host.
$show = "20"; //How many files do you want to show on the list
$template = "<span class=\"style1\"> {number}.<a href=\"{filelink}\">{filename} </a></span><br>";
//Template for each line.
//Don't modify anything below this line.
require $path. "/includes/mysql.php";
$pafiledb_sql->connect($db);
$config = $pafiledb_sql->query($db,"SELECT * FROM $db[prefix]_settings",1);
switch ($list) {
case newest:
$result = $pafiledb_sql->query($db, "SELECT * FROM $db[prefix]_files ORDER BY file_time DESC LIMIT 0,$show ", 0);
$info = "Added on {date}";
break;
case downloads:
$result = $pafiledb_sql->query($db, "SELECT * FROM $db[prefix]_files ORDER BY file_dls DESC LIMIT 0,$show ", 0);
$info = "{downloads}";
break;
case rating:
$result = $pafiledb_sql->query($db, "SELECT * FROM $db[prefix]_files ORDER BY file_rating/(file_totalvotes - 0) DESC LIMIT 0,$show", 0);
$info = "{rating}/10 - {votes} Votes";
break;
default:
die("You didn't select the type of list to show. Please see the paFileDB manual for more information");
break;
}
$i = 1;
while ($file = mysql_fetch_object($result)) {
$line = str_replace("{number}", $i, $template);
$line = str_replace("{filelink}", "$config[3]/pafiledb.php?action=file&id=$file->file_id", $line);
[b]$line = str_replace("{filename}", trimlen($file->file_name), $line);[/b]
    //$line = str_replace("{filename}", $file->file_name, $line);
if ($list == "newest") {  $infoline = str_replace("{date}", date("n/j/y", $file->file_time), $info); }
if ($list == "downloads") {$infoline = str_replace("{downloads}", $file->file_dls , $info); }
if ($list == "rating") {
$ntv = $file->file_totalvotes - 1;
$infoline = str_replace("{rating}", @round($file->file_rating/$ntv,2), $info);
$infoline = str_replace("{votes}", $ntv, $infoline);
}
$line = str_replace("{info}", $infoline, $line);
echo $line;
$i++;
}
?>[/quote]

Link to comment
Share on other sites

len() is not a PHP function. You need to be using strlen() instead:
[code]
<?php
function trimlen($String, $len = 20) {
  if (strlen($String) > $len) {
    $String = substr($String, 0, ($len - 3)) . '...';
  }
  return $String;
}
?>
[/code]

Notice, I also set an optional $len variable in your function that defaults to 20. This way, when you call the function, you can designate how many characters you wish to display.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.