我试图从URL解码的字符串中删除所有反斜杠,但它输出\而不是输出已删除的url解码字符串.
请你告诉我我的问题.
<?php
$json = $_GET['ingredients'];
echo urldecode(str_replace($json,$json, "\\"));
?>
Run Code Online (Sandbox Code Playgroud) 在PHP项目中,我有:
$string = "1,555";
str_replace(',', '', $string);
echo $string; //is still 1,555
Run Code Online (Sandbox Code Playgroud)
str_replace不会删除逗号.如果我var_dump.我明白了string(5) "1,555"
有任何想法吗?我只是需要删除逗号,以便我可以计算.
我正在尝试在C中创建一个函数来替换字符串中所有出现的子字符串.我创建了我的函数,但它只适用于较大字符串中第一次出现的子字符串.
这是迄今为止的代码:
void strreplace(char string[], char search[], char replace[]){
char buffer[100];
char*p = string;
while((p=strstr(p, search))){
strncpy(buffer, string, p-string);
buffer[p-string] = '\0'; //EDIT: THIS WAS MISSING
strcat(buffer, replace);
strcat(buffer, p+strlen(search));
strcpy(string, buffer);
p++;
}
}
Run Code Online (Sandbox Code Playgroud)
我不是C编程的新手,但我在这里缺少一些东西.
示例:输入字符串"marie has apples has",搜索"has"并替换为"blabla"
在第一个"has"被正确替换,但第二个没有.最终的输出是"marie blabla apples hasblabla".注意第二个"有"仍然存在.
我究竟做错了什么?:)
编辑现在正在运作.添加空终止字符可以解决问题.我知道结果字符串可能大于100.这是学校的作业,所以我不会有超过20左右的字符串.
我被freeCodeCamp的这个编码挑战Spinal Tap Case困住了。基本上我不知道如何执行最后一次检查。
这是最后一次检查:
spinalCase("AllThe-small Things") should return "all-the-small-things"
这是我的代码:
function spinalCase(str) {
var outputString,
newstr,
pattern1 = new RegExp(/[_\s]/, 'g'),
pattern2 = new RegExp(/(?=[A-Z])/, 'g'),
stringTest1 = pattern1.test(str),
stringTest2 = pattern2.test(str);
if(stringTest1) {
outputString = str.replace(pattern1, '-');
newstr = outputString.toLowerCase();
} else if(stringTest2) {
str.split(/(?=[A-Z])/).join(' ');
outputString = str.replace(pattern2, '-');
newstr = outputString.toLowerCase();
} else if (stringTest1 && stringTest2){
outputString = str.replace(pattern1, '-');
outputString = str.replace(pattern2, '-');
newstr = outputString.toLowerCase();
}
return newstr;
}
Run Code Online (Sandbox Code Playgroud)
我确实意识到最后一个else if …
我正在尝试修剪 R 字符串列表中的尾随方括号、内部引号和斜杠,最好使用dplyr.
样本数据:
df <- c("['Mamie Smith']", "[\"Screamin' Jay Hawkins\"]")
Run Code Online (Sandbox Code Playgroud)
预期结果:
"Mamie Smith", "Screamin' Jay Hawkins"
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
"Mamie Smith", "Screamin' Jay Hawkins"
Run Code Online (Sandbox Code Playgroud)
df %>%
str_replace("[[]]", "") # Also throws error
Run Code Online (Sandbox Code Playgroud) 来自PHP背景,我有点被宠坏了str_replace,你可以传递一系列干草堆和针.
我还没有在Javascript中看到过这样的功能,但是我已经设法完成了工作,尽管很难看,但下面显示的代码:
return myString.replace(" ", "-").replace("&", ",");
然而,由于我需要用另一个角色替换某些角色,我相信有更好的方法可以实现这一点 - 无论是性能还是更漂亮.
那么我该怎么做呢?
如何使用sprintf()一个值替换字符串中的多个占位符?我知道通常您会为每个占位符传递 1 个变量,但我知道如果您使用格式%1$s而不是%s可以传递单个值,但我似乎无法让它工作。
我实际上将其用作参数化的 sql 查询,但为了方便起见,这里有一个示例
$name = "Bill";
$string = "hello ?, your name is ? ";
$string = sprintf(str_replace("?","'%1$s'",$string ),$name);
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用。我还希望它适用于单个占位符,例如
$name = "Bill";
$string = "hello ?";
$string = sprintf(str_replace("?","'%1$s'",$string ),$name);
Run Code Online (Sandbox Code Playgroud)
注意:我具体不是在谈论使用传递数组,vsprintf()因为只有 1 个值。对我来说多次传递相同的值似乎毫无意义。
谢谢
I try to avoid asking stupid questions, but this for some reason is just not working. I'm retrieving some text from a database, with newlines included. When using echo straight away, this is the output:
=== BOLD TEXT ===\nHere is some text.
Run Code Online (Sandbox Code Playgroud)
This is how i retrieved it:
$row = $query->row();
$content=$row->course_content;
Run Code Online (Sandbox Code Playgroud)
以下都没有任何影响......
$content= str_replace(array("\r\n", "\r", "\n"), "<br />", $content);
$content= str_replace("\n", " ", $content);
$content = nl2br($content);
Run Code Online (Sandbox Code Playgroud)
但是,当我将它硬编码为原始语句的字符串时,
$content="=== BOLD TEXT ===\nHere is some text.";
Run Code Online (Sandbox Code Playgroud)
砰!它有效......关于为什么它拒绝接受数据库输入的任何想法,但手动字符串没问题?
完整代码:
//Switch between …Run Code Online (Sandbox Code Playgroud) set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
SET memoli=%token:QMZ=%NL%%%
echo %memoli%>>%tmp%\list2.txt
Run Code Online (Sandbox Code Playgroud)
我无法用新行更改字符串“QMZ”。怎么做?
我正在尝试使用strrchr()and用“and”替换文本中最后一次出现的逗号str_replace()。
例子:
$likes = 'Apple, Samsung, Microsoft';
$likes = str_replace(strrchr($likes, ','), ' and ', $likes);
Run Code Online (Sandbox Code Playgroud)
但这会替换整个最后一个单词(在本例中为 Microsoft),包括此字符串中的最后一个逗号。我怎样才能删除最后一个逗号并将其替换为 " 和 " ?
我需要使用strrchr()作为函数来解决这个问题。这就是为什么这个问题没有重复而且更具体的原因。
str-replace ×10
php ×5
string ×4
regex ×3
javascript ×2
replace ×2
batch-file ×1
c ×1
codeigniter ×1
comma ×1
dplyr ×1
gsub ×1
printf ×1
r ×1
urldecode ×1