Forum Moderators: coopster

Message Too Old, No Replies

php loop over string !

I want to edit mysql "text" field

         

smagdy

6:29 pm on May 24, 2005 (gmt 0)

10+ Year Member



Hi,

I have mysql "text" field, I got it into php string.

and its totally unformmated.

I put <pre> <? echo "$mysql_text";?> </pre>

and it was formatted but not as i really want..

so now I want to make new line every

( 50 characters or when there is '-')

so how to do it?

Thanks in advance

mcibor

9:09 pm on May 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Tell me, wouldn't it be better to store in db how the text is formatted? I recognise, that you have some articles/novels stored in your db. So when the author enters the new text then convert it to html (add tags).

However the new line before "-" you can do with:

$str = "And so she cried
- What are we goin' to do?";
$str = $str_replace("\n- ", "<br>- ", $str);// \n is a special character meaning the new line, which in html is <br>

However I myself would do that before placing the text into db (and letting, as it is done on this forum, to preview the text before submitting)

Hope this puts you in some direction
Michal Cibor

smagdy

7:46 am on May 25, 2005 (gmt 0)

10+ Year Member



Hi, thanks for your help but...

I already have 70 rows filled, so it will be some pain to reformate them all.

so I just want to be able to alter the the string by making newline after like 50 characters..

so is there a way for that?

Thanks

S.H

smagdy

12:00 pm on May 25, 2005 (gmt 0)

10+ Year Member



Please anybody help!

smagdy

12:36 pm on May 25, 2005 (gmt 0)

10+ Year Member



I meant

"so I just want to be able to alter the the string by making newline after like EVERY 50 characters"

mcibor

8:27 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How is the text stored in the db?
Was it put from the input textarea?

If yes, then do
$text = str_replace("\n", "\n<br>", $text);

what you want to do is hard to make. you can do:

for($i = 0; $i < strlen($text); $i + 51)
{
$text_corrected .= substr($text, $i, 50)."<br>";//you may have to check if it's not eating away one character
}

more on substr you can find at [php.net...]

Hope this helps
Michal Cibor

PS. This is a very bad and untidy method