将所有出现的角色替换为另一个角色的有效方法是什么std::string?
我想使用str_replace或类似的替代方法来替换JavaScript中的一些文本.
var text = "this is some sample text that i want to replace";
var new_text = replace_in_javascript("want", "dont want", text);
document.write("new_text");
Run Code Online (Sandbox Code Playgroud)
应该给
this is some sample text that i dont want to replace
Run Code Online (Sandbox Code Playgroud)
如果您要使用正则表达式,与内置替换方法相比,性能影响是什么.
我记得以前这样做过,但找不到代码.我使用str_replace来替换这样的一个字符:str_replace(':', ' ', $string);但我想替换所有以下字符\/:*?"<>|,而不是为每个字符执行str_replace.
我很难理解什么时候strtr会更好,str_replace反之亦然.看起来使用任一函数都可以获得完全相同的结果,尽管替换子串的顺序是相反的.例如:
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 string')."\n";
echo str_replace(array('st', 't', 's'), array('Z', 'Y', 'X'), 'test string');
Run Code Online (Sandbox Code Playgroud)
这输出
YeXY XYring
YeZ Zring
YeXY XYring
YeZ Zring
Run Code Online (Sandbox Code Playgroud)
除了语法之外,使用one还是其他有什么好处吗?任何人不足以达到预期结果的情况?
我有一个叫做的字符串 persons.name
我想.用/*/ 我的输出替换DOTpersons/*/name
我试过这段代码:
String a="\\*\\";
str=xpath.replaceAll("\\.", a);
Run Code Online (Sandbox Code Playgroud)
我收到了StringIndexOutOfBoundsException.
如何更换圆点?
有没有理由我没有看到,为什么这不起作用?
$string = $someLongUserGeneratedString;
$replaced = str_replace(' ', '_', $string);
echo $replaced;
Run Code Online (Sandbox Code Playgroud)
输出仍然包含空格......任何想法都会很棒
如何爆炸以下字符串:
Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor
Run Code Online (Sandbox Code Playgroud)
成
array("Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor")
Run Code Online (Sandbox Code Playgroud)
因此引用中的文本被视为单个单词.
这就是我现在所拥有的:
$mytext = "Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor"
$noquotes = str_replace("%22", "", $mytext");
$newarray = explode(" ", $noquotes);
Run Code Online (Sandbox Code Playgroud)
但我的代码将每个单词分成一个数组.如何将引号内的单词视为一个单词?
我希望能够替换空格,-但也想删除逗号和问号.我怎么能在一个函数中做到这一点?
到目前为止,我有它替换空格:
str_replace(" ","-",$title)
Run Code Online (Sandbox Code Playgroud) 我想在我的树枝模板中做一个简单的str_replace.我是twig的新手,可能我需要添加新的过滤器或类似的东西或使用现有的.我怎样才能做到这一点?我在哪里可以找到可用的过滤器列表?
str_replace使用数组时,我遇到了PHP函数的麻烦.
我有这样的信息:
$message = strtolower("L rzzo rwldd ty esp mtdsza'd szdepw ty esp opgtw'd dple");
Run Code Online (Sandbox Code Playgroud)
我试图这样使用str_replace:
$new_message = str_replace(
array('l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k'),
array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'),
$message);
Run Code Online (Sandbox Code Playgroud)
结果应该是A good glass in the bishop's hostel in the devil's seat,但相反,我得到了p voos vlpss xn twt qxswop's wosttl xn twt stvxl's stpt.
但是,当我只尝试更换2个字母时,它会很好地替换它们:
$new_message = str_replace(array('l','p'), array('a','e'), $message);
Run Code Online (Sandbox Code Playgroud)
字母l和p将被替换a和e.
如果它们的大小完全相同,为什么它不能使用完整的字母数组呢?