PHP str_replace 无法正常工作 [英] PHP str_replace not working correctly

查看:25
本文介绍了PHP str_replace 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 str_replace 但它无法正常工作.我有一个文本区域,该区域与表单一起发送.当服务器收到数据时,我想将新行更改为,".

I'm using str_replace and it's not working correctly. I have a text area, which input is sent with a form. When the data is received by the server, I want to change the new lines to ",".

$teams = $_GET["teams"];
$teams = str_replace("\n",",",$teams);
echo $teams;

奇怪的是,我收到以下结果

Strangely, I receive the following result

切尔西

,真实

,巴塞罗那

而不是Chealsea,real,Barcelona.

instead of Chealsea,real,Barcelona.

怎么了?

推荐答案

为了扩展 Waage 的响应,您可以使用数组来替换两组字符

To expand on Waage's response, you could use an array to replace both sets of characters

$teams = str_replace(array("\r\n", "\n"),",",$teams);
echo $teams;

这应该可以正确处理这两个项目,因为单个 \n 是有效的,如果您只是替换 \r\n

This should handle both items properly, as a single \n is valid and would not get caught if you were just replacing \r\n

这篇关于PHP str_replace 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆