Regex will replace the reg trademark

I need help with regex:

I have html output and need to wrap all registered trademarks with <sup></sup>

I am unable to insert the tag <sup>

in the title and alt

properties, and obviously I don't need to wrap the cases that were already superscripted.

The following regex matches text that is not part of the HTML tag:

(?<=^|>)[^><]+?(?=<|$)

      

An example of what I'm looking for:

$original = `<div>asd&reg; asdasd. asd<sup>&reg;</sup>asd <img alt="qwe&reg;qwe" /></div>`

      

The filtered string should be output:

<div>asd<sup>&reg;</sup> asdasd. asd<sup>&reg;</sup>asd <img alt="qwe&reg;qwe" /></div>

      

Thanks a lot for your time !!!

+2


source to share


4 answers


Well, here's an easy way if you agree with the following limitation:

Registers that have already been processed have </sup> following immediately after & reg;

echo preg_replace('#&reg;(?!\s*</sup>|[^<]*>)#','<sup>&reg;</sup>', $s);

      



Logics:

  • we only replace those & reg; not followed by </sup> and ...
  • not followed by> Symbol without opening <symbol
+3


source


I would use an HTML parser instead of regular expressions, since HTML is not regular and will present more cases with edges than you can dream of (ignoring your contextual constraints you mentioned above).



You don't say what technology you are using. If you post this, someone might undoubtedly recommend an appropriate parser.

+3


source


Regex is not enough for what you want. First you have to write code to detect when content is an attribute value or text of a node element. Then you have to go through all this content and use some kind of replacement method. I'm not sure what it is in PHP, but in JavaScript it looks something like this:

content[i].replace(/\&reg;/g, "<sup>&reg;</sup>");

      

0


source


I agree with Brian that regex is not a good way to parse HTML, but if you have to use regex you can try to split the string into tokens and then run a regex for each token.

I use preg_split

to split the string into HTML tags as well as a phrase <sup>&reg</sup>

- this will leave text that is either no longer superscript &reg;

or tagged as tokens. Then for each token &reg;

can be replaced with <sup>&reg;</sup>

:

$regex = '/(<sup>&reg;<\/sup>|<.*?>)/i';
$original = '<div>asd&reg; asdasd. asd<sup>&reg;</sup>asd <img alt="qwe&reg;qwe" /></div>';

// we need to capture the tags so that the string can be rebuilt
$tokens = preg_split($regex, $original, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
/* $tokens => Array
(
    [0] => <div>
    [1] => asd&reg; asdasd. asd
    [2] => <sup>&reg;</sup>
    [3] => asd
    [4] => <img alt="qwe&reg;qwe" />
    [5] => </div>
)
*/

foreach ($tokens as &$token)
{
    if ($token[0] == "<") continue; // Skip tokens that are tags
    $token = substr_replace('&reg;', '<sup>&reg;</sup>');
}

$tokens = join("", $tokens); // reassemble the string
// $tokens => "<div>asd<sup>&reg;</sup> asdasd. asd<sup>&reg;</sup>asd <img alt="qwe&reg;qwe" /></div>"

      

Note that this is a naive approach, and if the result is not formatted as expected, it may not be parsed the way you would like it to (again, regex is not good for parsing HTML;))

0


source







All Articles