从PHP中的字符串中删除单引号 [英] Removing single-quote from a string in php
问题描述
我有一个HTML表单,用户可以将文本输入到title
字段中,然后用php创建一个名为title.html
I have an HTML form that a user can input text into a title
field, I then have php creating an HTML file called title.html
我的问题是用户可以在html文件名中不能使用的标题字段中输入空格和撇号.我使用以下命令将空格替换为下划线:
My problem is that users can input spaces and apostrophes into the title field that can't be used in the html file name. I replaced the spaces with underscores by using:
$FileName = str_replace(" ", "_", $UserInput);
但是,我似乎无法删除单引号?我尝试使用:
However, I can't seem to remove single-quotes? I have tried using:
$FileName = preg_replace("/'/", '', $UserInput);
但这花了test's
并将其变成了test\s.html
.
but this took test's
and turned it into test\s.html
.
推荐答案
使用当前的str_replace方法:
Using your current str_replace method:
$FileName = str_replace("'", "", $UserInput);
虽然很难看到,但第一个参数是双引号,然后是单引号和双引号.第二个参数是两个双引号,中间没有任何引号.
While it's hard to see, the first argument is a double quote followed by a single quote followed by a double quote. The second argument is two double quotes with nothing in between.
使用str_replace,您甚至可以拥有要完全删除的字符串数组:
With str_replace, you could even have an array of strings you want to remove entirely:
$remove[] = "'";
$remove[] = '"';
$remove[] = "-"; // just as another example
$FileName = str_replace( $remove, "", $UserInput );
这篇关于从PHP中的字符串中删除单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!