Jump to content

manipulate text from database


dannybrazil

Recommended Posts

hello

 

does some one knows how can i make the text quierd from my databas to show lets say 10(char) and ....

 

example :

 

instead of :  hello my name is danny and i love football

 

i want  :  hello my name is danny...

 

and its a link so when they click it it goes to another page that they can see it all

 

thanks

Link to comment
Share on other sites

Well, you would probably want to include some logic to only show elipses if the value goes beyond 10 characters. Also, when using that functionality it is typically done only at word breaks - otherwise you might have some unintended consequences:

 

<?php
$text = "I need an association between point A and point B";
echo substr($str, 0, 13) . '...';
//Output "I need an ass..."
?>

 

To do this properly you would need to decide that if 10 characters (or whatever you use) will break a word, do you want to take one less word or one more word.

 

This code will take the last word that meets or exceeds the length given

<?php

function partialString($input, $length) {

  $words = explode(' ', $input);
  $output = '';

  while ((strlen($output)+1)<=$length && count($words)>0) {
    $output .= array_shift($words) . ' ';
  }

  $output = trim($output);
  $elpise = (strlen($output)<strlen($input))?' ...':'';

  return $output . $elpise;
}

$str = 'hello my name is danny and i love football';
echo partialString($str, 10);

//Output "hello my name ..."
?>

Link to comment
Share on other sites

hello

 

does some one knows how can i make the text quierd from my databas to show lets say 10(char) and ....

 

example :

 

instead of :  hello my name is danny and i love football

 

i want  :  hello my name is danny...

 

and its a link so when they click it it goes to another page that they can see it all

 

thanks

 

use substring() directly in the query and you wouldn't need to do any php logic.

Link to comment
Share on other sites

use substring() directly in the query and you wouldn't need to do any php logic.

 

But, will you know whether the string was shortened or not so you know whether or not to show the elipses? Also, it won't prevent words from being cut inappropriately.

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.