16

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.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhi
  • 5,501
  • 17
  • 78
  • 133

7 Answers7

35

Have a look at str_replace

$abc = ' Hello "Guys" , Goodmorning';
$abc = str_replace('"', '$^', $abc);
Krista K
  • 21,503
  • 3
  • 31
  • 43
bradley.ayers
  • 37,165
  • 14
  • 93
  • 99
8
str_replace('"','$^',$abc);

Should work for you.

S L
  • 14,262
  • 17
  • 77
  • 116
mike
  • 147
  • 2
  • 9
4
$abc = ' Hello "Guys" , Goodmorning';

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

output:

Hello $^Guys$^ , Goodmorning

Wh1T3h4Ck5
  • 8,399
  • 9
  • 59
  • 79
3
preg_replace('/"/', '$^', $abc);
Chris Bornhoft
  • 4,195
  • 4
  • 37
  • 55
1

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

str_replace('"', '$^', $abc);
deceze
  • 510,633
  • 85
  • 743
  • 889
1

You can use str_replace:

$abc = ' Hello "Guys" , Goodmorning';
echo str_replace('"','$^',$abc);
Bjoern
  • 15,934
  • 4
  • 43
  • 48
0

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);
Vishal Rana
  • 53
  • 1
  • 1
  • 10