Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: how can I prepend a string?

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,082
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default how can I prepend a string?

    how do you prepend a value to a string.
    For example

    $a='great';
    prepend 'pop ' to $a;
    $a == 'pop great';

    sort of like the reverse of .=

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    So I don't get what you mean.
    When you use .= have the text go in the beginning not front?
    Jeremy | jfein.net

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,082
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default

    yes.

    Code:
    $a='what';
    $a.=' if?';
    //$a becomes 'what if?';
    What I want to do is the opposite. I want to know if there is some sort of function or way that I can prepend the string so that you can add 'so ' to $a to make $a become 'so what if?'.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Well, why dont you just:
    $a='what';
    $a.=$a.' if?';
    //$a becomes 'what if?';
    Jeremy | jfein.net

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,082
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default

    because $a already has a pregenerated value. Nice thought though. I just found this simple answer though
    $a='if?';
    $b='what ';
    $a="$b$a";

    //$a becomes "what if?"
    Last edited by james438; 02-25-2008 at 03:37 AM. Reason: typo

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Isn't that just like what I posted?
    EDIT: Try what I posted.
    Jeremy | jfein.net

  7. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,082
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default

    No. You appended the string. I prepended the string. Since it is short here is the code that I am using it in:
    Code:
    <?php
    $a=0;$b='0000000';
    foreach ($_POST as $field[] => $value[])
    {
    if (strpos($field[$a],'check')===0) 
    else $value[$a]="$b$value[$a]";
    echo "$field[$a] = $value[$a]<br>";$a++;
    }
    
    echo "pp $_POST[loot_Lasl] pp";
    ?>
    to do what you suggested would erase the passed value, which is why I needed to prepend the string.

    P.S. the script is only in its infancy, so no need to critique it. I am merely designing it right now.

  8. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    o then you would judt:
    PHP Code:
    $a='what';
    $a=$a.' if?';
    //$a becomes 'what if?'; 
    Jeremy | jfein.net

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $a = ' if?';
    $a = 'What' . $a;
    $a .= $b is just a short form of $a = $a . $b.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,082
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default

    that works too

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •