Page 1 of 1

string problem

Posted: Sun May 25, 2008 8:54 am
by itsmani1
Hello all

I want to read sting and do some processing.

for example here is my sample string.
"this is my test string
new line"

I want to read this string and then i want to add a "-" at the end of every line, is there way to do this?

result should be like this:
"this is my test string-new line"


thank you
dizyn

Re: string problem

Posted: Sun May 25, 2008 6:02 pm
by Jaxolotl
The most simple thing you may use is the function str_replace()

Code: Select all

 
$original_string = "this is my test string
new line";
 
$processed_string = str_replace("\n","-",$original_string);
 
but you may use a lot of functions and then combine them as you need, it depends on the complexity of the process you need to do

see the manual
# preg_ replace_ callback()
# preg_ replace()
# preg_ split()
# ereg_ replace()
# eregi_ replace()
# str_replace()
# str_ireplace()

# explode() then combined with implode()

Re: string problem

Posted: Mon May 26, 2008 1:01 am
by GeertDD
You may want to normalize newlines before doing a str_replace on "\n".

Code: Select all

$str = str_replace(array("\r\n", "\r"), "\n", $str);