0

In php what is a function to only display strings that have a length greater than 50 characters, truncate it to not display more than 130 characters and limit it to one result?

so for example say i have 30 rows in a result set but I only want to show the newest row that have these parameters. If the newest row has 25 characters it should not display. It should only display the newest one that has a string length of 50 or more characters.

3
  • You've given us the parameters, but you've forgotten to indicate how you're getting the data. Commented Nov 30, 2010 at 3:54
  • As Ignactio points out, we need to know where your data is coming from. If it's coming from a database, you should do all of this in the query, not in PHP. Commented Nov 30, 2010 at 3:57
  • it is coming from a database. i just want to show only one result and that one result can only have a string length of more than 50. the problem is since i don't set a limit to 1 in the mysql query it displays all the results. should i create an if statement to only show the rows containing the string length of 50 or more characters? Commented Nov 30, 2010 at 4:05

2 Answers 2

1

Use an SQL query. For finding the newest you want max on either an auto_increment primary key (ill call it id) or a date/time when the row was created (say, time time_created).

So I am assuming table with: id (int), stringVal (string, char(), varchar(), whatever)

SELECT MAX(id),  SUBSTRING(stringVal, 1, 130)
FROM yourTable
WHERE LENGTH(stringVal) > 30

Replace id with a time field if you have to. You're going to have a hard time finding the newest without one of them, but you can always arbitrarily pick one row.

--Edit-- a sample of using mysql functions in PHP to run above query and fetch desired output

$sql = "SELECT MAX(id),  SUBSTRING(stringVal, 1, 130) FROM yourTable WHERE LENGTH(stringVal) > 30";
$r = mysql_query($sql, $conn); //im hoping $conn or something like it is already set up
$row = mysql_fetch_assoc($r);
$desiredString = $row['stringVal'];
Sign up to request clarification or add additional context in comments.

3 Comments

looks like string positions start with 1, weird. here's the string functions if you like to look for yourself dev.mysql.com/doc/refman/5.0/en/…
looks like the query works when i test it in phpmyadmin but there is no output in my script.
make sure you form the string correctly, die it out if you have to. then ensure you call your mysql functions properly (either mysql or mysqli) ill edit in a brief example
0

Something like this should do just make sure that you grab your data sorting by the newest items first. The break statement will ensure that the loop is terminated after the first result matching your criteria is found...

foreach($array_returned_from_query as $row)
{
    if(strlen($row) > 50)
    {
        echo substr($row, 0, 130);
        break;
    }
}

2 Comments

this is agood local solution but he's getting it from database so this would be best handled in query. he wasnt that clear until a few comments so not really your fault
If you were getting an invalid argument for foreach then you must have been passing an empty array. If you want to use the loop just check to make sure that the array isn't empty before looping through. Sounds like the query that @jon_darkstar posted would be a better solution for you though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.