php 简单的PHP字符串替换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5644947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple PHP string replace?
提问by Abhi
I have this code:
我有这个代码:
$abc = ' Hello "Guys" , Goodmorning';
I want to replace every occurrence of "
(double quotes) by $^
so that string becomes
我想替换"
(双引号)的每次出现,$^
以便字符串变为
'Hello $^Guys$^ , Goodmorning'
I am new to PHP; in Java we can do this very easily by calling the string class replaceAll
function, but how do I do it in PHP? I can't find the easy way on Google without using regular expressions.
我是 PHP 新手;在 Java 中,我们可以通过调用 string 类replaceAll
函数很容易地做到这一点,但我如何在 PHP 中做到这一点?如果不使用正则表达式,我在 Google 上找不到简单的方法。
What is some syntax with or without the use of regular expressions?
使用或不使用正则表达式的语法是什么?
回答by bradley.ayers
Have a look at str_replace
看一下 str_replace
$abc = ' Hello "Guys" , Goodmorning';
$abc = str_replace('"', '$^', $abc);
回答by mike
str_replace('"','$^',$abc);
Should work for you.
应该为你工作。
回答by Wh1T3h4Ck5
$abc = ' Hello "Guys" , Goodmorning';
$new_string = str_replace("\"", '$^', $abc);
echo $new_string;
output:
输出:
Hello $^Guys$^ , Goodmorning
你好$^伙计们$^,早上好
回答by Chris Bornhoft
preg_replace('/"/', '$^', $abc);
回答by deceze
Searching the manualwould have brought you to this: http://php.net/manual/en/function.str-replace.php
搜索手册会给你带来这个:http: //php.net/manual/en/function.str-replace.php
str_replace('"', '$^', $abc);
回答by Bjoern
You can use str_replace
:
您可以使用str_replace
:
$abc = ' Hello "Guys" , Goodmorning';
echo str_replace('"','$^',$abc);
回答by jazzmatazz2005
preg_replace('/"/', '$^', $abc);
should work easily for you
preg_replace('/"/', '$^', $abc);
应该很容易为你工作
回答by Vishal Rana
String replace function is used to replace string. Your syntax is wrong. You have to use php string functionlike below example. First of all, think about first and two values and replace in third. Let's have a look.
字符串替换函数用于替换字符串。你的语法是错误的。您必须使用如下示例的php 字符串函数。首先,考虑第一个和两个值并替换第三个。我们来看一下。
<?php
echo str_replace("Hello", "HI", "Hello Hyman ");
?>
It produces the output.
它产生输出。
HI Hyman.
You can create an HTML form and change on button only.
您可以创建 HTML 表单并仅在按钮上进行更改。
<form>
input box 1
input box 2
input box 3
button
</form>
Another
其他
str_replace('"','$^',$var);