用新行替换所有出现的句号,除非它是在 PHP 的电子邮件中

问题描述 投票:0回答:1

尝试找到一个解决方案来替换句子中所有出现的句号,除非是电子邮件。 例如

输入:

您好,感谢您联系我们,我们非常感谢您的反馈!我们一直在努力改进我们的产品,以便为您提供更好的体验。我一定会将您的建议转达给我们的团队,供他们在我们前进的过程中考虑。如果您有任何疑问,请致电 XYZ 12345 或发送电子邮件至 [email protected]。谢谢您的电子邮件。

预期输出:

您好,感谢您联系我们,我们非常感谢您的反馈!我们一直在努力改进我们的产品,为您提供更好的体验。

我一定会将您的建议转达给我们的团队,供他们在我们推进此事时考虑。

如有任何疑问,请致电 XYZ 12345 或发送电子邮件至 [email protected]

感谢您的电子邮件

php regex replace preg-replace
1个回答
0
投票
我希望这对你有帮助:

<?php $input = "Hello, Thanks for reaching us, we really appreciate your feedback! We are always trying to improve our product to provide you with a better experience.I will be sure to pass your suggestion onto our team for their consideration as we move forward with this. Call XYZ on 12345 or email at [email protected] if you have any question. Thank you for your email."; $pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b/'; preg_match_all($pattern, $input, $matches); $email = $matches[0][0]; $emailpos = strpos($input, $email); $output = ''; $input = str_replace($email,'',$input); $sentences = preg_split('/(?<=[.!?])\s+/', $input); foreach ($sentences as $sentence) { $output .= rtrim($sentence, '.') . "\n\n"; } $output = substr_replace($output, ' '.$email, $emailpos, 0); echo $output; ?>
    
© www.soinside.com 2019 - 2024. All rights reserved.