PHP str_replace

試著忘記壹切 提交于 2019-12-04 05:25:15

问题


I have the string $var in which I need to replace some text. The first "X" needs to be replaced by "A", the second "X" needs to be replaced by B and so on, here is an example:

<?php
$var = "X X X X"; // input
...
echo $var //the result: "A B C D"
?>

I tried with str_replace but that doesn't work.

Thanks


回答1:


You could use preg_replace's limit argument to only replace once.

<?php
    $var = 'X X X X';
    $replace = array('A', 'B', 'C', 'D');
    foreach($replace as $r)
        $var = preg_replace('/X/', $r, $var, 1);
    echo $var;
?>

http://codepad.viper-7.com/ra9ulA




回答2:


You could use preg_replace_callback():

// as many as you think you'll need, maximum.
// this can be programmatically generated, if need be
$replacements = array('A', 'B', 'C', 'D', 'E', 'F', 'G'); // and so on    

function get_replace_value($matches) {
    global $replacements;
    return array_shift($replacements);
}

$var = preg_replace_callback("/" + preg_quote($needle) + "/",
    "get_replace_value", $var);



回答3:


$var = 'X X X X';
$replacements = array('A', 'B', 'C', 'D');

$var = preg_replace_callback('/X/', function() use (&$replacements) {
    return array_shift($replacements);
}, $var);

Other solution:

$var = preg_replace('/X/', 'A', $var, 1);
$var = preg_replace('/X/', 'B', $var, 1);
$var = preg_replace('/X/', 'C', $var, 1);
$var = preg_replace('/X/', 'D', $var, 1);

This one uses the $limit parameter of preg_replace (we replace only one occurrence per call).




回答4:


Without use of regex

$arr = explode('X', 'X X X X');
$rep = array('A','B','C','D');
foreach ($arr as $idx=>$val)
{
  $arr[$idx] = $val.$rep[$idx];
}
echo implode($arr);



回答5:


Yet one more solution (more for a dynamic number of Xs):

<?php

  $foo = 'X X X X X X';

  $Xs = preg_match_all('/\bX\b/',$foo,$_); if ($Xs === false) $Xs = 0;
  $alphabet = range('A', chr(64 + $Xs));
  foreach ($alphabet as $letter){
    $foo = preg_replace('/\bX\b/', $letter, $foo, 1);
  }

  echo $foo;

I also added the \b in to the pattern to only replace Xs that are free-standing, so "FAUX PAS X" only replaces the last X.

demo

alphabet_replace (more modular form)




回答6:


Lets give it a shot, too, just for the heck of it ;)

$input = "X X X X";

function replace($input, array $replacements)
{
    $replacer = function(array &$i, array &$r, &$o) use(&$replacer) {
        if (count($i) === 0 || count($r) === 0) return;

        $i_cur = array_shift($i);

        if (ctype_space($i_cur)) $o .= $i_cur;
        else $o .= array_shift($r);

        $replacer($i, $r, $o);
    };

    $replacer(str_split($input), $replacements, $output);

    return $output;
}

var_dump(replace($input, range('A', 'Z'))); 



回答7:


Lots of loops are happening in the other answers, here's an alternative.

$var = 'X X X X X X X';
$replace = array('A', 'B', 'C', 'D');
$var = preg_replace(array_fill(0, count($replace), '/X/'), $replace, $var, 1);
echo $var; // A B C D X X X


来源:https://stackoverflow.com/questions/7390289/php-str-replace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!