str-replace
How can I use a dictionary to do multiple search-and-replace operations? [duplicate]
address = “123 north anywhere street” for word, initial in {“NORTH”: “N”, “SOUTH”: “S”}.items(): address = address.replace(word.lower(), initial) print(address) nice and concise and readable too.
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” }
str_replace in Twig
Use this to replace | with – and replace , width .: {{age|replace({‘|’: “-“, “,” : “.”})}} Example input 31|60, comes out as 31-60.
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);
PHP str_replace replace spaces with underscores
I’ll suggest that you use this as it will check for both single and multiple occurrence of white space (as suggested by Lucas Green). $journalName = preg_replace(‘/\s+/’, ‘_’, $journalName); instead of: $journalName = str_replace(‘ ‘, ‘_’, $journalName);
can you write a str.replace() using dictionary values in Python?
address = “123 north anywhere street” for word, initial in {“NORTH”:”N”, “SOUTH”:”S” }.items(): address = address.replace(word.lower(), initial) print address nice and concise and readable too.
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)