Jump to content

[SOLVED] Alright, str_replace is being a b****!


Mathy

Recommended Posts

$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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
  }
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.