Mathy Posted June 25, 2007 Share Posted June 25, 2007 $titl=$dat['title']; $crea=$dat['creator']; $subj=$dat['subject']; $foru=$dat['forum']; $tit=str_replace(" ", "-", $titl); $cre=str_replace(" ", "-", $crea); $sub=str_replace(" ", "-", $subj); $for=str_replace(" ", "-", $foru); $dat['title'] contains "Hmmm! Another test!" $dat['creator'] contains "Mathy" $dat['subject'] contains "Yup, testing again!" $dat['forum'] contains "Test forum 1" Why doesn't all the final variables replace the spaces in the text with lines? Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/ Share on other sites More sharing options...
Caesar Posted June 25, 2007 Share Posted June 25, 2007 Look at your code logic. PHP is going to work from the top to the bottom. Where exactly are you redefining the values for the $dat array before you print out it's value at the end of your code? Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282369 Share on other sites More sharing options...
Mathy Posted June 25, 2007 Author Share Posted June 25, 2007 That's not your business, when I tell you what the values contain. And yes, I know they contain that, because they all print out normally! However, with spaces and not "-" which it should be doing! The array is being assigned from a SQL database. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282376 Share on other sites More sharing options...
mattkenefick Posted June 25, 2007 Share Posted June 25, 2007 whats the actual code Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282397 Share on other sites More sharing options...
Mathy Posted June 25, 2007 Author Share Posted June 25, 2007 One second. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282399 Share on other sites More sharing options...
sKunKbad Posted June 25, 2007 Share Posted June 25, 2007 Try putting double quotes around your variables in the str_replace? Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282429 Share on other sites More sharing options...
trq Posted June 25, 2007 Share Posted June 25, 2007 Try putting double quotes around your variables in the str_replace? Variables do not need to be surrounded by double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282435 Share on other sites More sharing options...
Caesar Posted June 25, 2007 Share Posted June 25, 2007 That's not your business, when I tell you what the values contain. And yes, I know they contain that, because they all print out normally! However, with spaces and not "-" which it should be doing! The array is being assigned from a SQL database. I don't think you're getting it. If the flow/logic in your code is just like that snippet you showed, then think about it....you're doing the string replace after the array values have been defined....and printing out any of the $dat isn't going to go through the string replacing. Of course, that is going only by the small snippet of code you showed. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282440 Share on other sites More sharing options...
Caesar Posted June 25, 2007 Share Posted June 25, 2007 Scrap this.... $titl=$dat['title']; $crea=$dat['creator']; $subj=$dat['subject']; $foru=$dat['forum']; $tit=str_replace(" ", "-", $titl); $cre=str_replace(" ", "-", $crea); $sub=str_replace(" ", "-", $subj); $for=str_replace(" ", "-", $foru); And try this... <?php function do_it($dat) { foreach($dat as $newdat) { $newdat = str_replace(" ", "-", $newdat); } return $newdat; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282447 Share on other sites More sharing options...
btherl Posted June 26, 2007 Share Posted June 26, 2007 Mathy, not sure if this will help, but I hope it does. If you already understand this, then please disregard. Equality in programming is very different from equality in maths. In programming, the "=" sign actually means "assign" or "copy". If you say "$a = $b", then you are saying "Let $a be a copy of $b". If you then modify $a, you will not be modifying $b. $b remains with its original value. In your case, $dat['title'] does not change when you modify $titl, because $titl is only a copy of $dat['title']. So you must copy the final modified value back with the line $dat['title'] = $tit; And similarly for each other value. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282498 Share on other sites More sharing options...
Mathy Posted June 26, 2007 Author Share Posted June 26, 2007 Mathy, not sure if this will help, but I hope it does. If you already understand this, then please disregard. Equality in programming is very different from equality in maths. In programming, the "=" sign actually means "assign" or "copy". If you say "$a = $b", then you are saying "Let $a be a copy of $b". If you then modify $a, you will not be modifying $b. $b remains with its original value. In your case, $dat['title'] does not change when you modify $titl, because $titl is only a copy of $dat['title']. So you must copy the final modified value back with the line $dat['title'] = $tit; And similarly for each other value. Nice, I think you at least know what you're doing. Thanks to everyone else that helped so far, BUT! Can you please give an example to this code? I don't believe I am getting it. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282826 Share on other sites More sharing options...
suma237 Posted June 26, 2007 Share Posted June 26, 2007 hey, you need linebreak instead of '-'? Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282831 Share on other sites More sharing options...
Mathy Posted June 26, 2007 Author Share Posted June 26, 2007 No. "-" is what I need. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282950 Share on other sites More sharing options...
redarrow Posted June 26, 2007 Share Posted June 26, 2007 did this work? <?php function do_it($dat) { foreach($dat as $newdat) { $newdat = str_replace(" ", "-", $newdat); } return $newdat; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282955 Share on other sites More sharing options...
kalivos Posted June 26, 2007 Share Posted June 26, 2007 I didn't know you could make a variable with the name of "for". Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282959 Share on other sites More sharing options...
Mathy Posted June 26, 2007 Author Share Posted June 26, 2007 did this work? <?php function do_it($dat) { foreach($dat as $newdat) { $newdat = str_replace(" ", "-", $newdat); } return $newdat; } ?> Your code is useless, didn't even try it! Think about it! It will only actually return the last value of the array, not them all. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282960 Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 Your code is useless, didn't even try it! Drop the attitude buddy, your no better or you wouldnt be looking for help. People are here to try and help. That last piece of code could easily be moded to do what your after. <?php function do_it(&$dat) { foreach ($dat as $k => $v) { $dat[$k] = str_replace(" ", "-", $v); } } $a = array('this is foo','and this is bar'); do_it($a); print_r($a); ?> Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-282993 Share on other sites More sharing options...
Mathy Posted June 26, 2007 Author Share Posted June 26, 2007 None of your so far suggestions worked. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-283116 Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 My last post will replace spaces within array values with - given an array. If this is not what you want, what is? Post your current code, a description of what you expect to happen, and what is actually happening. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-283126 Share on other sites More sharing options...
Mathy Posted June 26, 2007 Author Share Posted June 26, 2007 Well yes, that part works, when I print it out, but when I then use $dat['title'] it's not with lines instead of spaces again! Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-283159 Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 Show me your code. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-283161 Share on other sites More sharing options...
sKunKbad Posted June 26, 2007 Share Posted June 26, 2007 I'd think a dood with 7K+ posts is probably right. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-283166 Share on other sites More sharing options...
chocopi Posted June 26, 2007 Share Posted June 26, 2007 Well yes, that part works, when I print it out, but when I then use $dat['title'] it's not with lines instead of spaces again! Of course using dat['title'] is going to give the string with spaces $titl=$dat['title']; $crea=$dat['creator']; $subj=$dat['subject']; $foru=$dat['forum']; $tit=str_replace(" ", "-", $titl); $cre=str_replace(" ", "-", $crea); $sub=str_replace(" ", "-", $subj); $for=str_replace(" ", "-", $foru); you have set the string with spaces to $titl, you are the using the str_replace on it and setting it to $tit, so get the string without the spaces you need to use $tit echo $dat['title']; // Hello my name is chocopi echo $tit; // Hello-my-name-is-chocopi ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-283193 Share on other sites More sharing options...
wildteen88 Posted June 26, 2007 Share Posted June 26, 2007 Mathy, not sure if this will help, but I hope it does. If you already understand this, then please disregard. Equality in programming is very different from equality in maths. In programming, the "=" sign actually means "assign" or "copy". If you say "$a = $b", then you are saying "Let $a be a copy of $b". If you then modify $a, you will not be modifying $b. $b remains with its original value. In your case, $dat['title'] does not change when you modify $titl, because $titl is only a copy of $dat['title']. So you must copy the final modified value back with the line $dat['title'] = $tit; And similarly for each other value. Nice, I think you at least know what you're doing. Thanks to everyone else that helped so far, BUT! Can you please give an example to this code? I don't believe I am getting it. Mathy, You'd do this: <?php // get the variables $title = $dat['title']; $creator =$dat['creator']; $subject = $dat['subject']; $forum = $dat['forum']; // You must re-assign $dat its new value in order for // spaces to be changed in to dashes $dat['title'] = str_replace(" ", "-", $title); $dat['creator'] = str_replace(" ", "-", $creator); $dat['subject'] = str_replace(" ", "-", $subject); $dat['forum'] = str_replace(" ", "-", $forum); ?> In the code you supplied earlier all you where doing is creating multiple instances (copies) of $dat and not doing anything with the created variables. Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-283222 Share on other sites More sharing options...
Mathy Posted June 26, 2007 Author Share Posted June 26, 2007 Still doesn't work. The whole code is: $title = $dat['title']; $creator =$dat['creator']; $subject = $dat['subject']; $forum = $dat['forum']; $dat['title'] = str_replace(" ", "-", $title); $dat['creator'] = str_replace(" ", "-", $creator); $dat['subject'] = str_replace(" ", "-", $subject); $dat['forum'] = str_replace(" ", "-", $forum); $data=$dat['title']; echo $data; $Data returns a string with spaces :S Quote Link to comment https://forums.phpfreaks.com/topic/57143-solved-alright-str_replace-is-being-a-b/#findComment-283364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.