Page 1 of 2
splitting a variable
Posted: Wed Dec 29, 2004 9:33 pm
by shiznatix
if i get a output of
some text here :: more text here
and it is 1 valriable is there any way to split it up kinda so i can control the 1st half as a variable and the 2nd half as a seperate variable? i know its kinda a vauge question but im not sure how i can elaborate much more but if u need me to i will try
Posted: Wed Dec 29, 2004 9:37 pm
by rehfeld
look at explode()
Posted: Wed Dec 29, 2004 9:44 pm
by feyd
- [php_man]explode()[/php_man]
- [php_man]preg_split()[/php_man]
- [php_man]split()[/php_man]
Posted: Thu Dec 30, 2004 9:20 am
by Robert Plank
Code: Select all
$string = "some text here :: more text here";
list($first, $second) = explode(" :: ", $string);
echo "first thing: $first, second thing: $second";
Posted: Thu Dec 30, 2004 3:42 pm
by shiznatix
aye thats just what i was looking for i just didnt know were to begin to look for that solution. you guys are the 1337.
Posted: Thu Dec 30, 2004 4:37 pm
by shiznatix
problems! the string is a value in a array so i did
Code: Select all
<?php
$array[$i] = $newstring2;
list($name4, $message4) = explode(" :: ", $newstring2);
fwrite($opened, "$name4 :: $message4");
?>
but it leaves the $name4 and $message4 as just nothing. is this because im turing a array value into a variable? can't i do that?
Posted: Thu Dec 30, 2004 4:41 pm
by Chris Corbyn
Posted: Thu Dec 30, 2004 4:44 pm
by shiznatix
full code looks like this -
Code: Select all
<?php
$file = "comments/$file";//get the file
$linenum = $_REQUEST['linenum'];//get the line number you want to edit
$newstring = $editedcom;//get the text you want to put in the line
$array = file($file);
$array[$linenum - 1] = $newstring;
$numberlines = count($array);
$opened = fopen ($file, "w+");
for ($i = 0; $i<$numberlines; $i++) {
$array[$i] = $newstring2;
list($name4, $message4) = explode(" :: ", $newstring2);
fwrite($opened, "$name4 :: $message4");
}
fclose($opened);
?>
Posted: Thu Dec 30, 2004 4:46 pm
by feyd
where's $editedcom come from? how's it set?
Posted: Thu Dec 30, 2004 4:56 pm
by shiznatix
Code: Select all
<?php
$txt = "comments/$file";
$opentxt = fopen($txt, 'r');
$i = 1;
while (!feof($opentxt)) {
$readtxt = fgetss($opentxt, 5000);
?>
<form action="admin.php?id=editcomments2&file=<? print $file; ?>" method="post">
<input type="text" name="editedcom" value="<? print $readtxt; ?>">
<input type="hidden" name="linenum" value="<? print $i; $i++; ?>">
<input type="submit" name="submit" value="Edit Text">
</form>
<br>
<?
}
?>
<?
fclose($opentxt);
?>
that is the form that outputs to
Code: Select all
<?php
$file = "comments/$file";//get the file
$linenum = $_REQUEST['linenum'];//get the line number you want to edit
$newstring = $editedcom;//get the text you want to put in the line
$array = file($file);
$array[$linenum - 1] = $newstring;
$numberlines = count($array);
$opened = fopen ($file, "w+");
for ($i = 0; $i<$numberlines; $i++) {
$array[$i] = $newstring2;
list($name4, $message4) = explode(" :: ", $newstring2);
fwrite($opened, "$name4 :: $message4");
}
fclose($opened);
?>
that is the complete code so if i missed somtin somewhere or whatever then tell me ppplease
Posted: Thu Dec 30, 2004 4:59 pm
by Robert Plank
Why isn't line 5
Code: Select all
$newstring = $_REQUEST['editedcom'];
instead?
Posted: Thu Dec 30, 2004 5:07 pm
by shiznatix
cause i got lazy and it really dosnt make much of a diffrence but it would be more stable so i just changed it but thats not going to solve my problem...
also just so whoever looks at my code knows i am completly new to arrays and i cant quite grasp the for loop command so bear with me please. and also when i go to edit the line it not only deletes that line it changes EVERY line in the file to just :: ??? but when i just use
Code: Select all
<?php
fwrite($opened, $array[$i])
?>
it works perfectly? AHHHHHHHH
Posted: Thu Dec 30, 2004 5:12 pm
by feyd
I think you swapped the order of variables on line 11.. as $newstring2 doesn't exist, and you are basically setting $array[x] to null.
Posted: Thu Dec 30, 2004 5:15 pm
by shiznatix
how would i go about not doing that? i was thinking of doing somtin like
Code: Select all
<?php
$array[$linenum - 1] = list($name4, $message4) = explode(" :: ", $newstring2)
?>
but that wouldnt work because of the 2 equal signs rite?
Posted: Thu Dec 30, 2004 5:26 pm
by feyd
Code: Select all
$newstring2 =& $arrayї$i];