Asked  7 Months ago    Answers:  5   Viewed   23 times

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?



Have a look at str_replace

$abc = ' Hello "Guys" , Goodmorning';
$abc = str_replace('"', '$^', $abc);
Wednesday, March 31, 2021
answered 7 Months ago

Actually the basic syntax for regular expressions, as supported by preg_replace and friends, is pretty easy to learn. Think of it as a string describing a pattern with certain characters having special meaning.

In your very simple case, a possible pattern is:


With d meaning a digit (numeric characters 0-9) and + meaning: Repeat the expression right before + (here: d) one or more times. All other characters just represent themselves.

Therefore, the pattern above matches any of the following strings:


Since the preg functions use a Perl-compatible syntax and regular expressions are denoted between slashes (/) in Perl, you have to surround the pattern in slashes:

$after = preg_replace('/&page-d+/', '', $before);

Actually, you can use other characters as well:

$after = preg_replace('#&page-d+#', '', $before);

For a full reference of supported syntax, see the PHP manual.

Wednesday, March 31, 2021
answered 7 Months ago

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario:

Related usability ticket:

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
answered 7 Months ago

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
answered 5 Months ago

Use preg_replace() with a limit of 1:

preg_replace('/nothing/', 'something', $str, 1);

Replace the regular expression /nothing/ with whatever string you want to search for. Since regular expressions are always evaluated left-to-right, this will always match the first instance.

Tuesday, August 3, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :