• PHP Help PHP Coding
  • How could I replace any spaces in this text and replace with "_" the underscore?

Hi all, can anyone please help with this:

With Google Adwords I can send keyword data by appending "?kw={keyword}" to the end of my destination url.

I can then echo on my page this keyword data by writing

<? echo $_GET['kw'] ?>

in my page.

How could I replace any spaces in this text and replace with "_" the underscore?

So say the keyword sent by google was "trout fishing" I would want to echo on page "trout_fishing".

Thanks

Mike

    [man]str_replace/man for a simple one-to-one replacement of " " with "_". For more complex replacements look at [man]preg_replace/man.

      <?php
      echo $_GET['kw'];
      // displays:
      // trout fishing
      echo str_replace( ' ', '_', $_GET['kw'] );
      // displays:
      // trout_fishing
      ?>
      

        Thank you. That's just what I needed.

        I appreciate your help.

        Regards

        Mike

          Write a Reply...