str-replace

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

倾然丶 夕夏残阳落幕 提交于 2019-11-26 10:29:58
How can I explode the following string: Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor into array("Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor") So that the text in quotation is treated as a single word. Here's what I have for now: $mytext = "Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor" $noquotes = str_replace("%22", "", $mytext"); $newarray = explode(" ", $noquotes); but my code divides each word into an array. How do I make words inside quotation marks treated as one word? You could use a preg_match_all(...) :

When to use strtr vs str_replace?

一曲冷凌霜 提交于 2019-11-26 05:24:49
问题 I\'m having a hard time understanding when strtr would be preferable to str_replace or vice versa. It seems that it\'s possible to achieve the exact same results using either function, although the order in which substrings are replaced is reversed. For example: echo strtr(\'test string\', \'st\', \'XY\').\"\\n\"; echo strtr(\'test string\', array( \'s\' => \'X\', \'t\' => \'Y\', \'st\' => \'Z\' )).\"\\n\"; echo str_replace(array(\'s\', \'t\', \'st\'), array(\'X\', \'Y\', \'Z\'), \'test

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

余生颓废 提交于 2019-11-26 02:09:24
问题 How can I explode the following string: Lorem ipsum \"dolor sit amet\" consectetur \"adipiscing elit\" dolor into array(\"Lorem\", \"ipsum\", \"dolor sit amet\", \"consectetur\", \"adipiscing elit\", \"dolor\") So that the text in quotation is treated as a single word. Here\'s what I have for now: $mytext = \"Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor\" $noquotes = str_replace(\"%22\", \"\", $mytext\"); $newarray = explode(\" \", $noquotes); but my code divides

PHP string replace match whole word

廉价感情. 提交于 2019-11-26 00:25:53
问题 I would like to replace just complete words using php Example : If I have $text = \"Hello hellol hello, Helloz\"; and I use $newtext = str_replace(\"Hello\",\'NEW\',$text); The new text should look like NEW hello1 hello, Helloz PHP returns NEW hello1 hello, NEWz Thanks. 回答1: You want to use regular expressions. The \b matches a word boundary. $text = preg_replace('/\bHello\b/', 'NEW', $text); If $text contains UTF-8 text, you'll have to add the Unicode modifier "u", so that non-latin

How to replace all occurrences of a character in string?

时光毁灭记忆、已成空白 提交于 2019-11-25 22:48:18
问题 What is the effective way to replace all occurrences of a character with another character in std::string ? 回答1: std::string doesn't contain such function but you could use stand-alone replace function from algorithm header. #include <algorithm> #include <string> void some_func() { std::string s = "example string"; std::replace( s.begin(), s.end(), 'x', 'y'); // replace all 'x' to 'y' } 回答2: I thought I'd toss in the boost solution as well: #include <boost/algorithm/string/replace.hpp> // in