Viewed   84 times

Is there a function in PHP that takes in a string, a number (i), and a character (x), then replaces the character at position (i) with (x)?

If not, can somebody help me in implementing it?

 Answers

5
$str    = 'bar';
$str[1] = 'A';
echo $str; // prints bAr

or you could use the library function substr_replace as:

$str = substr_replace($str,$char,$pos,1);
Sunday, October 2, 2022
 
4

You can make use of output bufferingDocs to get the output of that function:

ob_start();
get_header();
$html = ob_get_clean();

If you need that more than once, you can wrap it into a function of it's own:

/**
 * call a function and return it's output as string.
 * 
 * @param callback $function
 * @param array $arguments (optional)
 * @param var $return (optional) the return value of the callback
 * @return string function output
 */
function ob_get_call($function, array $arguments = array(), &$return = NULL)
{
    ob_start();
    $return = call_user_func_array($function, $arguments);
    $buffer = ob_get_clean();
    return $buffer;
}

Usage:

$html = ob_get_call('get_header');

As the answer is that popular today, here is another function to get the output of an include:

/**
 * include a file and return it's output as string.
 * 
 * @param string $file
 * @param array $variables (optional) keys as variable names and values as variable values
 * @param var $includeReturn (optional) the return value of the include
 * @return string function output
 */
function ob_get_include($file, array $variables = array(), &$includeReturn = NULL)
{
    $includeFilename = $file;
    unset($file);
    extract($variables);
    unset($variables);
    ob_start();
    $includeReturn = include($includeFilename);
    return ob_get_clean();
}

Usage:

include.php:

<div class="greeting">
    Hello <em><?php echo htmlspecialchars($name); ?></em>!
</div>

Using:

$variables = array(
    'name' => 'Marianne',
);
$html = ob_get_include('include.php', $vars);

Related:

  • Answer to *Load result of php code instead of the code as a string
  • Answer to Is include()/require() with “side effects” a bad practice?
Wednesday, September 7, 2022
1

Just do:

$row = str_replace("&", "&amp;", $row);

Note: Your foreach doesn't work because you need a reference, or use the key:

foreach ( $columns as &$value) { // reference
   $value  = str_replace("&", "&amp;", $value);
}
unset($value); // break the reference with the last element

Or:

foreach ($columns as $key => $value){
   $columns[$key]  = str_replace("&", "&amp;", $value);
}

Although it is not necessary here because str_replace accepts and returns arrays.

Sunday, August 7, 2022
 
4

[edit] sorry I couldn't help but do it the way I would if it were my project. A repeatable non-redundant process.

$array = [
    '<img src="emoticons/{{value}}" height="18" width="18">' => [
        ':)' => 'smile.png', 
        ';)' => 'wink.png' 
    ],
    '<br>' => ['n', 'r'],
    '****' => ['4lettercussword', '4lettercussword'],
    '*****' => '5lettercussword'
];

function filterText($array, &$msg) {
    foreach($array as $key => $value) {
        if(is_array($value)) {
           if(array_keys($value) !== range(0, count($value) - 1)) {
              foreach($value as $k => $v) {
                  $msg = str_replace($k, str_replace('{{value}}', $v, $key), $msg);
              }
           } else {
               for($i = 0;$i < count($value);$i++) {
                   $msg = str_replace($value[$i], $key, $msg);
               }
           }
        } else {
            $msg = str_replace($value, $key, $msg);
        }
    }
}

$msg = '4lettercussword :) n';
filterText($array, $msg);
echo $msg;

output:

**** <img src="emoticons/smile.png" height="18" width="18"> <br>

The key in the array is what will replace the value. If the key includes a {{value}} identifier then it knows the array pointed to will be associative, and that it needs to take the value from that array and plug it into the {{value}} identifier in your key. If any key equals a simple array of values it will replace any of those values with the key. This always you to have different html tags and replace only portions of it with a key value str_replace.

Thursday, October 27, 2022
 
julia
 
3

If you aren't required to use PHP, I would highly recommend performing stuff like this from the command line. It's by far the best tool for the job, and much easier to use.

In any case, the sed (Stream Editor) command is what you are looking for:

sed s/search/replace oldfilename > newfilename

If you need case-insensitivity:

sed s/search/replace/i oldfilename > newfilename

If you need this to perform dynamically within PHP, you can use passthru():

$output = passthru("sed s/$search/$replace $oldfilename > $newfilename");
Tuesday, November 8, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :