PHP在<a - tag之前添加空格

I need to add a whitespace before every

<a

in a string with php if there is not already a whitespace.

So this

hello world <a

should stay as it is

but this

hello world<a

should become the same as the first example.

Thanks for your input!

$text = preg_replace('/(?<!\s)<a/', ' <a', $text);

should do the trick

The (?<!\s) is called a negative look behind, only <a that do not have a whitespace before them will be replaced.