Jump to content

[SOLVED] Grab From Variable


Devine

Recommended Posts

Now say I have a variable that contains this information:

 

listen to me
whats your problem
i love heysuse

 

.. How can I ..

 

a) be able seperate it into 2 strings, say by having string one to be "listen to me" and the second to be the rest. orr, to seperate it by each string.

b) be able to add a new line to where I say under(or over) "listen to me"?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/
Share on other sites

actually the code i posted will generate the number of array base on the new line in the example you gave us its three line so you will have the 3 as length of array but take note that array index start at 0 so when you have the length of 3 the index of array will be 0 1 2

$string='listen to me
whats your problem
i love heysuse';
$array=explode('<br />',nl2br($string));
foreach ($array as $val) {
    echo $val . "\n<br />";
// you can put a condition here like
// if ($val == 'whats your problem') 
//etc
}

no no no, I already have something like that... but I want to get something to make it so... I have a variable in each stating the array [1] - [3] and there will be a new variable added somewhere on the line... so by that, I would then need to insert it somewhere in the lines.. meaning, I need to add each $variable in the for each ([1]-[3]) seperately, to make a new $variable at the end or after the for each statement..

 

which would state:

 

$variable="line from [1]
line from [2]
line inserted
line from [3]";

foreach($array as $val){
if($val="2"){
  $val2="blah";
}
}

 

Now with this, I want to add blah under "2" in the array itself... the array is exploded by lines.. meaning it would be like this:

 

2
3
seven
boobs

 

And now I want to remake a variable to be as the old array (before the explode) ..

 

So the variable used to be like above, and I want it now to be:

 

2
blah
3
seven
boobs

 

 

know what I mean now?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.