string manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nabanita
    New Member
    • Mar 2010
    • 9

    string manipulation

    How do I concatenate 2 or more string variables to 1 and then again split the 1 string variable into 2 or more?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the manual is always a good place to start: PHP string functions

    Comment

    • eragon
      Contributor
      • Mar 2007
      • 431

      #3
      Quite simple. Concentrating strings is easy as smushing them together. As far as splitting them, you can use many options. Here's some examples:

      [code=php]
      <?php

      // First off, we smush $foo and $bar together using a . between them:
      $foo = "Hello";
      $bar = "World!";

      $foobar = $foo.$bar; // Returns: HelloWorld;
      $spacebar = $foo.' '.$bar; // Returns: Hello World! (With a space)
      $dashbar = $foo.'-'.$bar; // Returns: Hello-World!
      // And we can go on forever... Moving on

      // Now, we can separate strings by a delimiter using explode();
      $foobar = "Hello World!";
      list($foo, $bar) = explode(" ", $foobar);
      // $foo is now "Hello" and $bar is now "World!"
      // Though, explode() will separate every space ever, and since our list doesn't have enough strings to hold it, stuff will vanish. You can also separate by substr();

      $foobar = "Hello World!";
      $foo = substr($foobar, 0, 5); // Sets $foo equal to the first five characters, starting on character 0 and ending on 5.
      $bar = substr($foobar, 6, 12); // And sets $bar equal to characters 6 through 12, making it "World!"
      [/code]

      And these are only a few ways to split up strings. I also suggest browsing php.net for the string functions. Also, if you can post specifically what you would like to do, I'm sure there's a method that'll work nicely.

      Comment

      Working...