Welcome Guest, Not a member yet? Register   Sign In
how add spaces in long string
#1

[eluser]nirbhab[/eluser]
hello,

Guys i am developing a blogging website in CI, i facing a problem,
when ever user enters a long string without spaces in it, it breaks the template, can any one suggest how to add spaces after certain length if user does so.

please reply.
#2

[eluser]xwero[/eluser]
Most of the time when i have little place to display possible long strings i use the css property overflow for that space and set it to auto or scroll.

It all depends on the type of the string, a word or a url or something else, and if the string can be seen in full elsewhere on the site.

I don't think it's a good idea to break off a string just to accommodate the design. If the design changes you have to be aware it's possible you have to change the php code too. But if you break off the string i think you do it best in the view file and to minimize the code in the view file you could create a plugin or a helper method.
#3

[eluser]nirbhab[/eluser]
when i know my string length, than formating becomes easier, but when user has to input his blog's post, we can't limit him to size.

but, i got exactly what i needed..thanks for reply dear, please look below what exactly i needed with its solution.
Code:
$string="THISPROGRAMISATESTTOSPLITTHELONGSTRINGINTOSMALLPARTSSOTHATITDOESN'TDESTROYESTHETEMPLATE";
echo chunk_split($string,10,'<br>');
#4

[eluser]xwero[/eluser]
That splits the word but you get

THISPROGRA
MISATESTTO
SPLITTHELO
...

Which is a cryptic block of text to most of the people that just glance the page. That is why i'm not so found about splitting a string using character length based chunks. Because most long words use hypens or are urls, to make them readable you could use this
Code:
function split_long_word($word,$end='<br>')
{
   $return = '';  
   if(preg_match('/^http/',$word)
   {
     $temp = substr($word,0,6); // removes http://
     $domain = strstr($word, '/', true);
     $return = '<a href="'.$word.'">'.$domain.'</a>';
   }
   else
   {
     $temp = explode('-',$word);
     $return = '';
     foreach($temp as $part){ $return .= $part.'-'.$end; }
   }
   return $return;
}

I think you let the exception become the rule.




Theme © iAndrew 2016 - Forum software by © MyBB