Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to use "str_replace"?

Comments in 'Plugin Development' started by korado531m7, Jan 14, 2016.

  1. korado531m7
    Offline

    korado531m7 Active Member

    Joined:
    Apr 4, 2015
    Posts:
    158
    Minecraft User:
    korado531m7
    Hi. Please tell me.
    Thank you for answering.
    Tim // robske Büba likes this.
  2. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    AndrewBit and applqpak like this.
  3. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Simple.
    PHP:
    $string '{SEARCH}';

    $string str_replace('{SEARCH}''{REPLACED}'$string);

    var_dump($string); //string(8) {REPLACE}
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Also note that the first two arguments, or just the first one, can be (a) string array(s).
    applqpak and Vaivez66 like this.
  5. korado531m7
    Offline

    korado531m7 Active Member

    Joined:
    Apr 4, 2015
    Posts:
    158
    Minecraft User:
    korado531m7
    Thank you for answering. I'll try.
  6. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Can also be a file,

    PHP:
    touch("random_file.txt");
    file_put_contents("random_file.txt""{SEARCH}");
    $file file_get_contents("random_file.txt");
    str_replace("{SEARCH}""{REPLACED}"$file);
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why mention it specifically... In that way, it can also be anything...
  8. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Btw, whenever you are not sure anymore about the usage, look it up on php.net, just search php str_replace
  9. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Because, I'm saying it can also be used to search/replace in a file, which is useful. And, yes, pretty much.
  10. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    I already give him the link -_-
    applqpak likes this.
  11. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Btw, this is a bit of code that allows you to replace strings found in an associative array.

    For example:
    PHP:
    <?php
    $replace 
    = array( 'dog' => 'cat',
    'apple' => 'orange',
    'chevy' => 'ford' );
    $string 'I like to eat an apple with my dog in my chevy';

    echo 
    str_replace_assoc($replace,$string); // Echo: I like to eat an orange with my cat in my ford
    Here is the function:
    PHP:
    <?php 
    function strReplaceAssoc(array $replace$subject) {
      return 
    str_replace(array_keys($replace), array_values($replace), $subject);  
    }
    ?>
    From www.php.net
    Last edited: Jan 16, 2016
    PEMapModder likes this.
  12. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    It was a tip, I am sorry for you that you cannot what I am saying

Share This Page

Advertisement