str-replace

PHP Simple way to replace or remove empty lines with str_replace

泪湿孤枕 提交于 2019-12-01 09:24:36
$line-out = str_replace('\r', '', str_replace('\n', '', $line-in)); The above works for me but, I saw a [\n\r] example somewhere and I cannot seem to find it. I just want to get rid any blank lines. The above is in a foreach loop. Thanks for teaching. Lekensteyn You shouldn't use - in variable names ;) $line_out = preg_replace('/[\n\r]+/', '', $line_in); $line_out = str_replace(array("\n", "\r"), '', $line_in); Manual entries: http://php.net/manual/en/function.preg-replace.php http://php.net/manual/en/function.str-replace.php str_replace can be passed an array as: $line_out = str_replace(array

Unexpected behavior from str_replace()

非 Y 不嫁゛ 提交于 2019-12-01 08:17:05
问题 I have two arrays. One with color names and the other with the RGB values. I am converting a color name to it's RGB value using str_replace() (then doing some other stuff with it). All of the colours work as expected, except Pale Yellow . $colour = "Pale Yellow"; $RGBint = array('Red' ,'Burgundy','Rust' ,'Electric Orange','Pumpkin' ,'Melon' ,'Baby Pink' ,'Candy Floss Pink','Electric Pink','Yellow' ,'Pale Yellow','Golden' ,'Lime' ,'Kiwi' ,'Mint' ,'Dragonfly Green','Kelly Green','Fern' ,'Forest

How to find and replace all occurrences of a substring in a string?

早过忘川 提交于 2019-11-30 18:35:15
I need to search a string and edit the formatting of it. So far I can replace the first occurrence of the string, but I am unable to do so with the next occurrences of this string. This is what I have working, sort of: if(chartDataString.find("*A") == string::npos){ return;} else{chartDataString.replace(chartDataString.find("*A"), 3,"[A]\n");} If it doesn't find the string, nothing prints at all, so that's not good. I know I need to loop through the entire string chartDataString and replace all occurrences. I know there are a lot of similar posts to this but I don't understand (like this

How to find and replace all occurrences of a substring in a string?

旧街凉风 提交于 2019-11-30 14:51:47
问题 I need to search a string and edit the formatting of it. So far I can replace the first occurrence of the string, but I am unable to do so with the next occurrences of this string. This is what I have working, sort of: if(chartDataString.find("*A") == string::npos){ return;} else{chartDataString.replace(chartDataString.find("*A"), 3,"[A]\n");} If it doesn't find the string, nothing prints at all, so that's not good. I know I need to loop through the entire string chartDataString and replace

Python string.replace() not replacing characters

醉酒当歌 提交于 2019-11-30 11:43:15
Some background information: We have an ancient web-based document database system where I work, almost entirely consisting of MS Office documents with the "normal" extensions (.doc, .xls, .ppt). They are all named based on some sort of arbitrary ID number (i.e. 1245.doc). We're switching to SharePoint and I need to rename all of these files and sort them into folders. I have a CSV file with all sorts of information (like which ID number corresponds to which document's title), so I'm using it to rename these files. I've written a short Python script that renames the ID number title. However,

Escaping escape Characters

时光怂恿深爱的人放手 提交于 2019-11-30 09:11:35
I'm trying to mimic the json_encode bitmask flags implemented in PHP 5.3.0, here is the string I have: $s = addslashes('O\'Rei"lly'); // O\'Rei\"lly Doing json_encode($s, JSON_HEX_APOS | JSON_HEX_QUOT) outputs the following: "O\\\u0027Rei\\\u0022lly" And I'm currently doing this in PHP versions older than 5.3.0: str_replace(array('\\"', "\\'"), array('\\u0022', '\\\u0027'), json_encode($s)) or str_replace(array('\\"', '\\\''), array('\\u0022', '\\\u0027'), json_encode($s)) Which correctly outputs the same result: "O\\\u0027Rei\\\u0022lly" I'm having trouble understanding why do I need to

I have a string with “\u00a0”, and I need to replace it with “” str_replace fails

南笙酒味 提交于 2019-11-30 08:00:00
问题 I need to clean a string that comes (copy/pasted) from various Microsoft Office suite applications (Excel, Access, and Word), each with its own set of encoding. I'm using json_encode for debugging purposes in order to being able to see every single encoded character. I'm able to clean everything I found so far (\r \n) with str_replace, but with \u00a0 I have no luck. $string = 'mail@mail.com\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0;mail@mail.com'; //this is

how to replace “(double quotes) in a string with \\” in java

三世轮回 提交于 2019-11-30 06:51:34
I have string variable strVar with value as ' "value1" ' and i want to replace all the double quotes in the value with ' \" ' . So after replacement value would look like ' \"value1\" ' How to do this in java? Kindly help me. You are looking for strVar = strVar.replace("\"", "\\\"") DEMO I would avoid using replaceAll since it uses regex syntax in description of what to replace and how to replace, which means that \ will have to be escaped in string "\\" but also in regex \\ (needs to be written as "\\\\" string) which means that we would need to use replaceAll("\"", "\\\\\""); or probably

PHP str_replace replace spaces with underscores

删除回忆录丶 提交于 2019-11-30 00:19:51
Is there a reason that I'm not seeing, why this doesn't work? $string = $someLongUserGeneratedString; $replaced = str_replace(' ', '_', $string); echo $replaced; The output still includes spaces... Any ideas would be awesome Laurent Brieu 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); Try this instead: $journalName = preg_replace('/\s+/', '_', $journalName); Explanation: you are most

Python string.replace() not replacing characters

佐手、 提交于 2019-11-29 17:33:36
问题 Some background information: We have an ancient web-based document database system where I work, almost entirely consisting of MS Office documents with the "normal" extensions (.doc, .xls, .ppt). They are all named based on some sort of arbitrary ID number (i.e. 1245.doc). We're switching to SharePoint and I need to rename all of these files and sort them into folders. I have a CSV file with all sorts of information (like which ID number corresponds to which document's title), so I'm using it