splitting a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

splitting a variable

Post 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
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

look at explode()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  • [php_man]explode()[/php_man]
  • [php_man]preg_split()[/php_man]
  • [php_man]split()[/php_man]
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post 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";
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

What is

Code: Select all

$i
defined as?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

where's $editedcom come from? how's it set?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Why isn't line 5

Code: Select all

$newstring = $_REQUEST['editedcom'];
instead?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$newstring2 =&amp; $array&#1111;$i];
Post Reply