I have the below simple replace working
<?php
$mydata= '15-2003';
$pattern = '/[-]/';
$replacement = ' ';
echo preg_replace($pattern, $replacement, $mydata);
?>
Which outputs 15 2003
However when I put it in my foreach loop it doesn’t seem to work?
I have this
<?php foreach ($tests as $test): ?>
<tr>
<?php
$mydata= htmlout($test['f']);
$pattern = '/[-]/';
$replacement = '';
echo preg_replace($pattern, $replacement, $mydata);
?>
<?php endforeach; ?>
Which outputs 15-2003
Where am I going wrong here?
htmlout is the below custom function.
<?php
function html($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
function htmlout($text)
{
echo html($text);
}
When I do var_dump($mydata);
I get NULL
This doesn’t work as intended because
htmlout()echoes the value instead ofreturning it.Consider replacing
with
What happends in your code is that it simply prints out the original string, returns
NULLto$mydataand then you echo aNULLwhich doesn’t show anything.