Home
Thursday, September 21, 2017
3:09:47 PM
Users online: 0   You are here >> Home > Programming

Forums | Programming Forums search
Forum FAQ
   
  1  
PHP strings
superfireydave 
24/7/08 10:14:24 AM
Titan

Not sure if this goes here or in web design but I'll go here as it's more about programming =P

I've got a string as an input.
I'm then saving it in a database.
There's a flash file that loads the string then displays it.


My issue is this: In the flash file, there's too many spaced.

For example something like this:
abcd
efgh
is spaced out once again like this:
abcd

efgh
It's annoying, but I've figured out that I can just use HTML code to make a new line and have it work that way.


However, I don't want the user to be able to input HTML when they enter the text (it's a content management system and the users not HTML savvy and I'd really like to minimise issues).



THEREFORE:
With PHP is it possible to take a string which is in this form:

 
$string = "this
is
my
string";


and change it to this:
 
$string = "this<br/>is<br/>my<br/>string";

?

I'm sure there has to be a way, but my searches have been fruitless and all about changing strings to integers and to dates :<


bah, I'm an idiot.
Go to check the last place I can think of and
str_ireplace

I'll see how it goes -_-


sigh, not working
maybe I just suck QQ


Edited by superfireydave: 24/7/2008 10:44:43 AM

-----
Mreow?
http://www.atomicmpc.com.au/forums.asp?s=2&c=9&t=17306

dave_blob 
24/7/08 11:13:40 AM
Guru

easiest way to do this in any language is probably with some form of regular expression substitution.

PHP:
ereg_replace('\s+','<br />', $string)

ta dah! Never done php myself but it took me literally 5 seconds to put together with google cause I knew what I was looking for!
Regular expressions, learn them!

EDIT: the first arg is the regular expression, it says match one or more(the '+') instances of whitespace( the \s ).


In ruby it would be:
newstring = string.gsub(/\s+/,'<br />')
or
string.gsub!(/\s+/,'<br />')
to do it inplace.


EDIT2:
Your webdev life will be alot easier if you learn regex, trust me!


Edited by dave_blob: 24/7/2008 11:20:08 AM

-----
Your comeback shames me

Phelan:
You exist. You are born and you die. That's it.
What matters is life before death - enjoy your time here, be nice to others and have some fun.

Girvo 
24/7/08 12:30:28 PM
Immortal

This should've been done already, Davey...


:P

-----
Quote by Disco
Edit: I got my own age wrong? o_0



superfireydave 
24/7/08 4:13:58 PM
Titan

Cheers =P
I know what a regular expression is :<
My tiredness doesn't QQ and I tried that earlier but to no avail I'll try again though =P

edit:

Nothing seems to work =|
I might have to look for a flash related fix :S


Edited by superfireydave: 24/7/2008 4:30:10 PM

-----
Mreow?
http://www.atomicmpc.com.au/forums.asp?s=2&c=9&t=17306

linke 
24/7/08 5:33:48 PM
Guru

Is this what you mean?

http://au2.php.net/nl2br

-----
If you're not part of the solution, you're part of the precipitate.

superfireydave 
24/7/08 6:29:07 PM
Titan

Quote by linke
Is this what you mean?

http://au2.php.net/nl2br


THAT DID IT!
I love you! <3!!!

-----
Mreow?
http://www.atomicmpc.com.au/forums.asp?s=2&c=9&t=17306

linke 
24/7/08 6:41:12 PM
Guru

Nice.

I had a similar experience a few years ago.

-----
If you're not part of the solution, you're part of the precipitate.

  1  
Forums | Programming