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
Share on other sites

$string='listen to me

whats your problem

i love heysuse';

$array=explode('<br />',nl2br($string));

$x=$array[0];

$y=$array[1];

$z=$array[2];

 

or use regular expression

 

How can I figure out how many lines there are? (or how many arrays there are?).

Link to comment
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
}

Link to comment
Share on other sites

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]";

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.