Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple PHP string replace? [duplicate]

Tags:

php

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?

like image 961
Abhi Avatar asked Apr 13 '11 05:04

Abhi


People also ask

How can I replace multiple words in a string in PHP?

Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.

How to replace a part of a string in PHP?

The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

What is str replace?

Python String replace() Method The replace() method replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.

How to replace values in PHP?

The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively.


2 Answers

Have a look at str_replace

$abc = ' Hello "Guys" , Goodmorning';
$abc = str_replace('"', '$^', $abc);
like image 77
bradley.ayers Avatar answered Sep 23 '22 13:09

bradley.ayers


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

Should work for you.

like image 33
mike Avatar answered Sep 23 '22 13:09

mike