Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String formatting

Status
Not open for further replies.

newbiepg

Programmer
Nov 6, 2002
181
IN
I have a couple of questions on strings.

First I need to select the first 500 characters in a string
without cutting off a word (approximately 500 will do)

Secondly I need to replace some characters in the string, for instance I need to replace a newline with a blank, and single quotes to two single quotes

How can these be done? I am using php 4.11
and the database is 7.22

Thanks in advance
 
Your comment "and the database is 7.22" needs some clarification.

By "7.22", do you mean "PostgreSQL 7.22" ?

Are these selections taking place in the database, or in variables in a PHP script?


About your question on replacing characters:

When you say "replace a newline with a blank", do you mean "replace a newline with a space"?

Do you need to replace every occurrance of a character with another give character, or do you need to replace a specific single character with a given character? Want the best answers? Ask the best questions: TANSTAAFL!
 
"By "7.22", do you mean "PostgreSQL 7.22" ?"

yes Postgresql 7.22
"Are these selections taking place in the database, or in variables in a PHP script? "
any ways really

"When you say "replace a newline with a blank", do you mean "replace a newline with a space"? "

yes


 
It's hard to advise without knowing what the nature of the text is. Normal ASCII prose text will be easier than HTML, for example.

One way would be to fetch the first 550 characters of the string from the database, then use preg_split () ( with the PREG_SPLIT_DELIM_CAPTURE flag to split the string on all spaces and punctuation.

I would then loop through the array produced, concatenating each element of the array onto a variable until the variable's length is 500 characters or until the addition of an element would put the length of the string over 500 characters.

Once I had my 500-or-less character string, I would then use str_replace () ( twice, once to replace newlines with spaces and a second time to replace single quotes with double quotes. Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top