Devine Posted August 2, 2007 Share Posted August 2, 2007 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 More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 $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 Link to comment https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-313628 Share on other sites More sharing options...
clearstatcache Posted August 2, 2007 Share Posted August 2, 2007 separate it by each string; $string='listen to me whats your problem i love heysuse'; $array= preg_split('/( +)|(\s+)/',$string); print_r($array); not sure bwt ds...just try... Link to comment https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-313701 Share on other sites More sharing options...
Devine Posted August 2, 2007 Author Share Posted August 2, 2007 $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 https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314256 Share on other sites More sharing options...
Devine Posted August 2, 2007 Author Share Posted August 2, 2007 orr, make a while statement and echo each line per loop? Link to comment https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314261 Share on other sites More sharing options...
deadimp Posted August 2, 2007 Share Posted August 2, 2007 How can I figure out how many lines there are? (or how many arrays there are?). Look at count() / sizeof() in the PHP Manual. It should be in the Array section, so you should see other functions for arrays there. Link to comment https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314265 Share on other sites More sharing options...
premiso Posted August 2, 2007 Share Posted August 2, 2007 orr, make a while statement and echo each line per loop? <?php foreach ($array as $val) { echo $val . "\n<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314272 Share on other sites More sharing options...
Devine Posted August 2, 2007 Author Share Posted August 2, 2007 how can I add a variable from each "for each" into one variable? Link to comment https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314416 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 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 https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314424 Share on other sites More sharing options...
Devine Posted August 2, 2007 Author Share Posted August 2, 2007 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 https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314434 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 what??? Link to comment https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314437 Share on other sites More sharing options...
Devine Posted August 3, 2007 Author Share Posted August 3, 2007 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 https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314528 Share on other sites More sharing options...
teng84 Posted August 3, 2007 Share Posted August 3, 2007 use the .= concatenator is that foreach($array as $val){ if($val="2"){ $val2="blah"; } $x .= $val; } hope you see the logic or am i wrong with that Link to comment https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314534 Share on other sites More sharing options...
Devine Posted August 3, 2007 Author Share Posted August 3, 2007 Works great, thanks. Link to comment https://forums.phpfreaks.com/topic/62977-solved-grab-from-variable/#findComment-314677 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.