我得到的图像文件在文件名中有捷克语字符(例如,ěščřžýáíé),我想重新命名它们而不需要重音符号,这样它们就更适合网络.我以为我可以使用一个简单的str_replace函数,但它似乎与文件数组的工作方式不同,它与字符串文字一样.
检查扩展后,我用readdir读取文件.
function readFiles($dir, $ext = false) {
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($ext){
if(end(explode('.', $file)) == $ext) {
$f[] = $file;
}
} else {
$f[] = $file;
}
}
closedir($dh);
return $f;
} else {
return false;
}
} else {
return false;
}
}
$files = readFiles(".", "jpg");
$search = array('š','á','ž','í','?','é','?','?','ý','?',' ');
$replace = array('s','a','z','i','e','e','r','n','y','c','-');
$string = "?š?áýísdjksnalci sáš?ééalskcnkkjy+?éší";
$safe_string = str_replace($search, $replace, $string);
echo '<pre>';
foreach($files …Run Code Online (Sandbox Code Playgroud) 我有一个脚本生成包含某些令牌的内容,我需要替换每个出现的令牌,其中不同的内容来自单独的循环.
使用str_replace简单地用相同的内容替换所有出现的令牌很简单,但是我需要用循环的下一个结果替换每个出现.
我确实看到了这个答案:在PHP5中搜索并用多个/不同的值替换多个值?
但它是从预定义的数组工作,我没有.
示例内容:
This is an example of %%token%% that might contain multiple instances of a particular
%%token%%, that need to each be replaced with a different piece of %%token%% generated
elsewhere.
Run Code Online (Sandbox Code Playgroud)
为了参数,我需要用生成的内容替换每次出现的%% token %%,这个简单的循环:
for($i=0;$i<3;$i++){
$token = rand(100,10000);
}
Run Code Online (Sandbox Code Playgroud)
因此,使用不同的随机数值$ token替换每个%%标记%%.
这个简单的东西,我只是没有看到?
谢谢!
好吧,我已经尝试过这些,但似乎它们都不起作用,我的示例字符串是
$text_description=" Hello world! lorel ipsum";
$text_description= str_replace(" "," ",$text_description);
$text_description = preg_replace("/&#?[a-z0-9]+;/i"," ",$text_description);
$text_description=html_entity_decode($text_description);
Run Code Online (Sandbox Code Playgroud) 我试图在Java String中替换" - "字符但是不起作用:
str.replace("\u2014", "");
Run Code Online (Sandbox Code Playgroud)
你可以帮帮我吗 ?
例如...
String s = "abcxyzabcxyz";
s.replaceAll("xyz", "---");
Run Code Online (Sandbox Code Playgroud)
这通常会给我"abc---abc---"。我怎么才能让它给我"---xyz---xyz"呢?
$ status的输出
Array
(
[1] => 1
[2] => 0
[3] => 0
[4] => 4
[5] => 4
)
$color_code_string = implode(",",$status);
Run Code Online (Sandbox Code Playgroud)
输出继电器
1,0,0,4,4
$color_code_string = str_replace("0","'#F00'",$color_code_string);
$color_code_string = str_replace("1","'#00bcd4'",$color_code_string);
$color_code_string = str_replace("2","'#4caf50'",$color_code_string);
$color_code_string = str_replace("3","'#bdbdbd'",$color_code_string);
$color_code_string = str_replace("4","'#ff9900'",$color_code_string);
Run Code Online (Sandbox Code Playgroud)
例外
SyntaxError: illegal character
colors: ['#00bcd'#ff9900'','#F00','#F00','#ff9900','#ff9900']
//prints '#00bcd'#ff9900'','#F00','#F00','#ff9900','#ff9900'
Run Code Online (Sandbox Code Playgroud)
如何实现预期输出如下
'#00bcd','#ff9900','#F00','#F00','#ff9900','#ff9900'
Run Code Online (Sandbox Code Playgroud) 是否有删除所有连字符、特殊字符等的通用正则表达式,所以我只会得到字母。
例如,包含:,./<>?;':"|[]{}-=_+1234567890!@#$%^&*()|\ ~` 以及所有连字符和特殊字符的正则表达式。
(不知道这是否称为正则表达式,但我希望你能明白)
我已经看到了一些答案(比如这个),但我有一些更复杂的场景,我不确定如何解释。
我基本上有完整的 HTML 文档。我需要用绝对 URL替换每个相对 URL。
来自潜在 HTML 的元素如下所示,也可能是其他情况:
<img src="/relative/url/img.jpg" />
<form action="/">
<form action="/contact-us/">
<a href='/relative/url/'>Note the Single Quote</a>
<img src="//example.com/protocol-relative-img.jpg" />
Run Code Online (Sandbox Code Playgroud)
期望的输出是:
// "//example.com/" is ideal, but "http(s)://example.com/" are acceptable
<img src="//example.com/relative/url/img.jpg" />
<form action="//example.com/">
<form action="//example.com/contact-us/">
<a href='//example.com/relative/url/'>Note the Single Quote</a>
<img src="//example.com/protocol-relative-img.jpg" /> <!-- Unmodified -->
Run Code Online (Sandbox Code Playgroud)
我不想替换协议相对 URL,因为它们已经用作绝对 URL。我想出了一些有效的代码,但我想知道是否可以稍微清理一下,因为它非常重复。
但我必须考虑单引号和双引号的属性值src, href, 和action(我是否遗漏了任何可以具有相对 URL 的属性?)同时避免协议相对 URL。
这是我到目前为止所拥有的:
// Make URL replacement protocol relative to not …Run Code Online (Sandbox Code Playgroud) 当我替换字符串的空格、点和逗号时,有时会出现双连字符。
例如转check out the 1. place成check-out-the-1--place
我怎样才能避免这种情况?我希望它是check-out-the-1-place- 这样每个单词之间只有一个连字符。这是我的代码:
str_replace([' ', ',', '.','?'], '-', strtolower($pathname));
Run Code Online (Sandbox Code Playgroud)
现在,我知道为什么它会返回双连字符,但我不知道如何解决这个问题。
有人可以帮我吗?
Imagine that I have a dataframe or datatable with strings column where one row looks like this:
a1; b: b1, b2, b3; c: c1, c2, c3; d: d1, d2, d3, d4
Run Code Online (Sandbox Code Playgroud)
and a look-up table with codes for mapping each of these strings. For example:
string code
a1 10
b1 20
b2 30
b3 40
c1 50
c2 60
...
Run Code Online (Sandbox Code Playgroud)
I would like to have a mapping function that maps this string to code:
10; b: 20, 30, 40; c: …Run Code Online (Sandbox Code Playgroud)