Reputation: 71
Can anyone see what i have done incorreclty using this class it should change the word before to after but it just sits there doing nothing. The second php pgm is the actual classs I am using..
<?php
include("parse.inc.php");
$bob = new parseClass();
$tag = "{Before}";
$replacement = "After";
$content = "My animal runs around all over the place and his name is {Before}.";
$bob->ReplaceTag($tag, $replacement, $content);
echo $bob;
?>
parse.inc.php (file name)
***************************
<?php
Class parseClass {
function ReplaceTag($tag, $replacement, $content) {
$content = str_replace($tag, $replacement, $content);
return $content;
}
}
// END Class parseClass
?>
Upvotes: 0
Views: 105
Reputation: 1
Simply replace these two lines
$bob->ReplaceTag($tag, $replacement, $content);
echo $bob;
with
$content = $bob->ReplaceTag($tag, $replacement, $content);
echo $content;
Upvotes: 0
Reputation: 9592
If the hard requirement is that you want to be able to echo
the instance of parseClass
, try this:
class parseClass
{
private $content;
public function ReplaceTag($tag, $replacement, $content)
{
$this->content = str_replace($tag, $replacement, $content);
}
public function __toString()
{
return $this->content;
}
}
$bob = new parseClass();
$tag = "{Before}";
$replacement = "After";
$content = "My animal runs around all over the place and his name is {Before}.";
$bob->ReplaceTag($tag, $replacement, $content);
echo $bob;
For reference, see:
Upvotes: 0
Reputation: 59
I guess you want to print the string having be replaced,so you should :
$result = $bob->ReplaceTag($tag, $replacement, $content);
echo $result;
Why are you use echo to print class?If you want to do this,please use "var_dump" or 'print_r' instead;
Upvotes: 0
Reputation: 47
In your case, you pass the content into class's function Therefore, you should store the return value into variable first
$result = $bob->ReplaceTag($tag, $replacement, $content);
echo $result;
Upvotes: 1
Reputation: 7535
You just need to store the result of your function in a local variable, and print that.
$result = $bob->ReplaceTag($x, $y, $z);
print($result);
Upvotes: 0