PHP in Moodle

I’ve only had limited experience using PHP and I need to use it to create a filter in Moodle. I’ve created this bit of code which converts the word colour to color:

[php]<?php
class filter_helloworld extends moodle_text_filter {
public function filter($text, array $options = array()) {
return str_replace(‘colour’, ‘color’, $text);
}
}
?>[/php]

How would I make it so it can change multiple words, for example, I have a list of about 200 words to change from GB English to US English. I tried this:

[php]<?php

class filter_helloworld extends moodle_text_filter {

public function filter($strings = array();

$strings['colour'] = 'color';

$strings['centre'] = 'center';) {

  foreach ($strings as $string1 => $string2) {

       $text = str_replace($string1, $string2, $text);

}

}

?>[/php]

But the page didn’t load :-X
Any help would be really appreciated!

Turn on error reporting in your development environment. Add this to the top of your file (or enable it in php.ini)

[php]error_reporting(E_ALL);
ini_set(‘display_errors’, 1);[/php]

This:

[php]$strings[‘colour’] = ‘color’;

  $strings['centre'] = 'center';) {

   foreach ($strings as $string1 => $string2) {

        $text = str_replace($string1, $string2, $text);

 }

[/php]
is a huge issue in a class. What are you expecting it to do?

Well to be honest, I’ve only just started playing with this stuff so I don’t really know what I’m doing! I’m just playing around until something works :-[

I followed this tutorial to create the first bit of code:

https://docs.moodle.org/dev/Filters

That creates a filter to change the word ‘hello’ to ‘hello world’.

What I would like to do is the same, only with a list of around 100 words, i.e. colour to color and centre to centre, etc.

I hope that makes sense!

Sponsor our Newsletter | Privacy Policy | Terms of Service