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?
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?
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:
Just do:
$row = str_replace("&", "&", $row);
Note: Your foreach doesn't work because you need a reference, or use the key:
foreach ( $columns as &$value) { // reference
$value = str_replace("&", "&", $value);
}
unset($value); // break the reference with the last element
Or:
foreach ($columns as $key => $value){
$columns[$key] = str_replace("&", "&", $value);
}
Although it is not necessary here because str_replace
accepts and returns arrays.
[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.
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");
or you could use the library function
substr_replace
as: