What regex pattern do I need for this?

前端 未结 5 669
不思量自难忘°
不思量自难忘° 2020-12-22 09:48

I need a regex (to work in PHP) to replace American English words in HTML with British English words. So color would be replaced by colour, meters by metres and so on [I kno

相关标签:
5条回答
  • 2020-12-22 10:01

    You do not want a regular expression for this. Regular expressions are by their very nature stateless, and you need some measure of state to be able to tell the difference between 'in a html tag' and 'in data'.

    You want to be using a HTML parser in combination with something like a str_replace, or even better, use a proper grammer dictionary and stuff as Lucero suggests.

    0 讨论(0)
  • 2020-12-22 10:01

    The second problem is easier - you want to replace when there are word boundaries around the word: http://www.regular-expressions.info/wordboundaries.html -- this will make sure you don't replace the meter in Brammeter.

    The first problem is much harder. You don't want to replace words inside HTML entities - nothing between <> characters. So, your match must make sure that you last saw > or nothing, but never just <. This is either hard, and requires some combination of lookahead/lookbehind assertions, or just plain impossible with regular expressions.

    a script implementing a state machine would work much better here.

    0 讨论(0)
  • 2020-12-22 10:02

    You don't need to use a regex explicitly. You can try the str_replace function, or if you need it to be case insensitive use the str_ireplace function.

    Example:

    $str = "<p>Color: red</p>";
    $new_str = str_ireplace ('%color%', 'colour', $str);
    

    You can pass an array with all the words that you want to search for, instead of the string.

    0 讨论(0)
  • 2020-12-22 10:22

    Html/xml should not be processed with regular expressions, it is really hard to generate one that will match anything. But you can use the builtin dom extension and process your string recursively:

    # Warning: untested code!
    function process($node, $replaceRules) {
        foreach ($node->children as $childNode) {
            if ($childNode instanceof DOMTextNode) {
                $text = pre_replace(
                    array_keys(replaceRules),
                    array_values($replaceRules),
                    $childNode->wholeText
                );
                $node->replaceChild($childNode, new DOMTextNode($text));
            } else {
                process($childNode, $replaceRules);
            }
        }
    }
    $replaceRules = array(
        '/\bcolor\b/i' => 'colour',
        '/\bmeter\b/i' => 'metre',
    );
    $doc = new DOMDocument();
    $doc->loadHtml($htmlString);
    process($doc, $replaceRules);
    $htmlString = $doc->saveHTML();
    
    0 讨论(0)
  • 2020-12-22 10:24

    I think you'd rather need a dictionary and maybe even some grammatical analysis in order to get this working correctly, since you don't have control over the input. A pure regex solution is not really going to be able to process this kind of data correctly.

    So I'd suggest to first come up with a list of words that need to be replaced, those are not only "color" and "meter". Wikipedia has some information on the topic.

    0 讨论(0)
提交回复
热议问题