2

Let's say I have this string:

$string = '<p > ¡Esto es una prueba! < /p > <p> <strong > Prueba 123 </strong> </p> <p> <strong> < a href="https://matricom.net"> MATRICOM < / a> </ strong> </p> <p> <strong > Todas las pruebas aquí ... </strong > < /p>'

What I want to do is fix the HTML tags using PHP (they are malformed due to the spaces). I have tried several different regex expressions that I have found online such as this:

$html = trim(preg_replace('/<\s+>/', '<>', $text));

and:

$html = preg_replace('/<(.+?)(?:»| |″)(.+?)>/', '<\1\2>', $text);

I am attempting to get a string output like this (spaces removed in front part and end part of HTML tags):

'<p> ¡Esto es una prueba! </p> <p> <strong> Prueba 123 </strong> </p> <p> <strong> <a href="https://matricom.net"> MATRICOM </a> </strong> </p> <p> <strong> Todas las pruebas aquí ... </strong> </p>'

Backstory: Google Translate has the tendency to add random spaces in translation results which affect HTML structure. Just looking for a quick way to clean the tags up. I have been searching for two days how to do this and can't seem to find anything that fits quite what I'm looking for.

2 Answers 2

1

In a most general case, you may use a preg_replace_callback solution:

$text='<p > ¡Esto es una prueba! < /p > <p> <strong > Prueba 123 </strong> </p> <p> <strong> <a href="https://matricom.net"> MATRICOM < / a> </ strong> </p> <p> <strong > Todas las pruebas aquí ... </strong > < /p>';
echo preg_replace_callback('~<[^<>]+>~u', function($m) { 
    return str_replace(' ', '', $m[0]); 
  // or,  preg_replace('~\s+~u', '', $m[0]); 
}, $text);

See the PHP demo.

However, you might want to create a pattern to only match the tags that are really used in Google Translate output. For a, p and strong tags it will look like

'~<\s*(?:/\s*)?(?:p|a|strong)\s*>~u'

See this regex demo

Details

  • < - < char
  • \s* - 0+ whitespaces
  • (?:/\s*)? - an optional sequence of / and then 0+ whitespaces
  • (?:p|a|strong) - p, a or strong substrings
  • \s* - 0+ whitespaces
  • > - a > char.
1
  • 1
    You are a life-saver! Thank you for the 2nd part, this fixed the issue with the "a href" space being removed. Brilliant. Thank you!
    – Kris B.
    Mar 13, 2020 at 23:03
0

This might be more than you need, but the process to translate HTML files (whether it's by MT or human) involves parsing the HTML through a filter which hides\protects HTML tags from the translation process entirely. The translation editor will only allow the movement of certain tags for linguistic purposes (in your example it could be href). Also, in some languages the bold formatting may not be desirable.

Once post-processed the HTML remains in place as it was with only the textual content changed.

Please note you may find with Google translate that the content of the HTML tags is sometimes also translated, which will cause you all sorts of issues.

I can explain the solution in more detail, just let me know if this is of interest. The tools required can be obtained freely.

3
  • What question are you answering?
    – Toto
    Mar 15, 2020 at 10:24
  • Perhaps it wasn't clear, but the issue of Google Translate adding spaces to HTML tags - I'm proposing a solution to prevent this from occurring at all.
    – Noel M
    Mar 15, 2020 at 12:43
  • I ended up separating the text from the HTML, translating the text, then recreating the DOM to solve this problem. It also reduces the character count that is sent to the translator. Although Witkor's answer fixed my immediate problem, it ended up becoming more complex later on so I adopted this more sophisticated solution.
    – Kris B.
    Mar 22, 2020 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.