string problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

string problem

Post 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
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Re: string problem

Post 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()
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: string problem

Post 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);
Post Reply