[PHP] Insert A String Before or After A Line In A File?

Zero3X

Acquaintance
Joined
Dec 4, 2009
Messages
26
Reaction score
0
FP$
6
Basically I want to insert a string (like "Hello World") before of after a line or string (lets say before "My Name is Zero3X" on line 22). How could I do this (sample code, tutorials... would be great).
 
What exactly do you mean, so it sais:

Hello World
My Name is Zero3X
 
Don't really understand what you mean. In this what you mean:
Code:
<?php
echo "Hello World";
?>
 
If you want to prepend a string you've already made, you can do this....

Code:
<?PHP
$string = "My Name is Zero3X";

echo "Hello World<BR />$string";
?>
Is that what you mean?
 
Wrong, Um, depends on how you have the first string.. you dont' need substring replace, since that is something that alters one string, here are two ways of doing it

the "." (period) is a connector between variables in PHP

Code:
<?php
$string1 = "This is my first string.";
$string2 = "This is my second string.";

// OUTPUT: This is my first string.This is my second string
echo = $string1.$string2;
?>

These are more ways
Code:
<?php
$string = "This is string part one";
$string = $string ." and this is the second part";

// OUTPUT: This is string part one and this is the second part
echo $string;
?>

Code:
<?php
$string2 = "and the second;

// OUTPUT: This is the first part of the string - and the second
echo "This is the first part of the sting - ". $string2;
?>

any other questions?
 
Just a comment about the following...
JamesD31 said:
Code:
<?php
$string = "This is string part one";
$string = $string ." and this is the second part";

// OUTPUT: This is string part one and this is the second part
echo $string;
?>

Can also be accomplished:
Code:
<?php
$string = "This is string part one";
$string .= " and this is the second part";

// OUTPUT: This is string part one and this is the second part
echo $string;
?>
Not a big deal, but it's shorter to write. 😉
 
imkingdavid said:
Just a comment about the following...
JamesD31 said:
Code:
<?php
$string = "This is string part one";
$string = $string ." and this is the second part";

// OUTPUT: This is string part one and this is the second part
echo $string;
?>

Can also be accomplished:
Code:
<?php
$string = "This is string part one";
$string .= " and this is the second part";

// OUTPUT: This is string part one and this is the second part
echo $string;
?>
Not a big deal, but it's shorter to write. 😉


I was going to do that.. but decided against it because they could have been like.. "wth?" and i was than going to comment it after what i put.. but forgot 😀
 
Back
Top Bottom