str_replace with array

前端 未结 5 1242
粉色の甜心
粉色の甜心 2020-12-01 23:33

I\'m having some troubles with the PHP function str_replace when using arrays.

I have this message:

$message = strtolower(\"L rzzo rwldd         


        
相关标签:
5条回答
  • 2020-12-01 23:56

    Easy and better than str_replace:

    <?php
    $arr = array(
        "http://" => "http://www.",
        "w" => "W",
        "d" => "D");
    
        $word = "http://desiweb.ir";
        echo strtr($word,$arr);
    ?>
    

    strtr PHP doc here

    0 讨论(0)
  • 2020-12-02 00:01

    If the text is a simple markup and has existing anchors, stage the existing anchor tags first, swap out the urls, then replace the staged markers.

    $text = '
    Lorem Ipsum is simply dummy text found by searching http://google.com/?q=lorem in your <a href=https://www.mozilla.org/en-US/firefox/>Firefox</a>,
    <a href="https://www.apple.com/safari/">Safari</a>, or https://www.google.com/chrome/ browser.
    
    Link replacements will first stage existing anchor tags, replace each with a marker, then swap out the remaining links.
    Links should be properly encoded.  If links are not separated from surrounding content like a trailing "." period then they it will be included in the link.
    Links that are not encoded properly may create a problem, so best to use this when you know the text you are processing is not mixed HTML.
    
    Example: http://google.com/i,m,complicate--d/index.html
    Example: https://www.google.com/chrome/?123&t=123
    Example: http://google.com/?q='. urlencode('<a href="http://google.com">http://google.com</a>') .'
    ';
    
    // Replace existing links with a marker
    $linkStore = array();
    $text = preg_replace_callback('/(<a.*?a>)/', function($match) use (&$linkStore){ $key = '__linkStore'.count($linkStore).'__'; $linkStore[$key] = $match[0]; return $key; }, $text);
    
    // Replace remaining URLs with an anchor tag
    $text = preg_replace_callback("/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/", function($match) use (&$linkStore){ return '<a href="'. $match[0] .'">'. $match[0] .'</a>'; }, $text);
    
    // Replace link markers with original
    $text = str_replace(array_keys($linkStore), array_values($linkStore), $text);
    
    echo '<pre>'.$text;
    
    0 讨论(0)
  • 2020-12-02 00:02

    str_replace with arrays just performs all the replacements sequentially. Use strtr instead to do them all at once:

    $new_message = strtr($message, 'lmnopq...', 'abcdef...');
    
    0 讨论(0)
  • 2020-12-02 00:05

    Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements.

        // Outputs F because A is replaced with B, then B is replaced with C, and so on...
        // Finally E is replaced with F, because of left to right replacements.
        $search  = array('A', 'B', 'C', 'D', 'E');
        $replace = array('B', 'C', 'D', 'E', 'F');
        $subject = 'A';
        echo str_replace($search, $replace, $subject);
    
    0 讨论(0)
  • 2020-12-02 00:10

    Alternatively to the answer marked as correct, if you have to replace words instead of chars you can do it with this piece of code :

    $query = "INSERT INTO my_table VALUES (?, ?, ?, ?);";
    $values = Array("apple", "oranges", "mangos", "papayas");
    foreach (array_fill(0, count($values), '?') as $key => $wildcard) {
        $query = substr_replace($query, '"'.$values[$key].'"', strpos($query, $wildcard), strlen($wildcard));
    }
    echo $query;
    

    Demo here : http://sandbox.onlinephpfunctions.com/code/56de88aef7eece3d199d57a863974b84a7224fd7

    0 讨论(0)
提交回复
热议问题