开发者

Simple PHP string replace? [duplicate]

This question already has answers here: How do I replace certain parts of my string? (5 answers) Closed 2 years ago.

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 str开发者_如何学Going class replaceAll function, but how do I do it in PHP? I can't find the easy way on Google without using regular expressions.

What is some syntax with or without the use of regular expressions?


Have a look at str_replace

$abc = ' Hello "Guys" , Goodmorning';
$abc = str_replace('"', '$^', $abc);


str_replace('"','$^',$abc);

Should work for you.


$abc = ' Hello "Guys" , Goodmorning';

$new_string = str_replace("\"", '$^', $abc);
echo $new_string;

output:

Hello $^Guys$^ , Goodmorning


preg_replace('/"/', '$^', $abc);


Searching the manual would have brought you to this: http://php.net/manual/en/function.str-replace.php

str_replace('"', '$^', $abc);


You can use str_replace:

$abc = ' Hello "Guys" , Goodmorning';
echo str_replace('"','$^',$abc);


String replace function is used to replace string. Your syntax is wrong. You have to use php string function like below example. First of all, think about first and two values and replace in third. Let's have a look.

<?php
echo str_replace("Hello", "HI", "Hello Jack "); 
?>

It produces the output.

HI Jack.

You can create an HTML form and change on button only.

<form> 
input box 1 
input box 2 
input box 3 
button 

</form>

Another

 str_replace('"','$^',$var);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜