str_replace with array

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. // Outputs F because A is replaced with B, then B is replaced with C, and so on… // Finally E is replaced with F, because of left to right replacements. $search = array(‘A’, ‘B’, ‘C’, ‘D’, ‘E’); … Read more

PHP explode the string, but treat words in quotes as a single word

This would have been much easier with str_getcsv(). $test=”Lorem ipsum “dolor sit amet” consectetur “adipiscing elit” dolor”; var_dump(str_getcsv($test, ‘ ‘)); Gives you array(6) { [0]=> string(5) “Lorem” [1]=> string(5) “ipsum” [2]=> string(14) “dolor sit amet” [3]=> string(11) “consectetur” [4]=> string(15) “adipiscing elit” [5]=> string(5) “dolor” }

How to replace multiple items from a text string in PHP? [duplicate]

You can pass arrays as parameters to str_replace(). Check the manual. // Provides: You should eat pizza, beer, and ice cream every day $phrase = “You should eat fruits, vegetables, and fiber every day.”; $healthy = [“fruits”, “vegetables”, “fiber”]; $yummy = [“pizza”, “beer”, “ice cream”]; $newPhrase = str_replace($healthy, $yummy, $phrase);

How to Replace dot (.) in a string in Java

You need two backslashes before the dot, one to escape the slash so it gets through, and the other to escape the dot so it becomes literal. Forward slashes and asterisk are treated literal. str=xpath.replaceAll(“\\.”, “/*/”); //replaces a literal . with /*/ http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)