我有一个长字符串,可以同时保存所有这些值:
hello<!>how are you? <!>I am fine<!> What is up? <!> Nothing!
Run Code Online (Sandbox Code Playgroud)
我需要找到所有这些可能性:
' <!> '
' <!>'
'<!> '
'<!>'
Run Code Online (Sandbox Code Playgroud)
并用"\n"替换它们
可以用php中的str_replace实现吗?
嗨,我尝试做这样的事情:
我有一些字符串 - 'Hello World!'例如.我想要替换除了第一个和白色空格之外的所有字符.
所以......结果将是:"H.... ......"; 我不想删除它,只是替换"."或其他字符.
我尝试过这样做,preg_replace但没有结果.
我注意到str_replace函数的一个奇怪的行为.你能告诉我为什么在no_2中取而代之的是3 号我得到no_4?
情况如下:
$pattern = array(1,2,3);
$change = array(1,3,4);
$sql = "SELECT * FROM %s WHERE no_1 IN (%s) AND no_2 IN (%s) AND no_3 IN (%s)";
$test_multiply[] = str_replace($pattern, $change, $sql);
Run Code Online (Sandbox Code Playgroud)
这使:
Array ( [0] => SELECT * FROM %s WHERE no_1 IN (%s) AND no_4 IN (%s) AND no_4 IN (%s) )
Run Code Online (Sandbox Code Playgroud)
你能告诉我怎么办才能收到no_3而不是no_2?
我有一个名为path的变量,例如url
www.google.co.uk%3Fq%3Dde
Run Code Online (Sandbox Code Playgroud)
我正在通过2 str_replace运行正确格式化.
$path = str_replace('%3F', '?', $path);
$path = str_replace ('%3D', '=', $path);
Run Code Online (Sandbox Code Playgroud)
然后我打印输出,但仍然打印" %3F "和" %3D ".当我使用这两个str_replace时,这确实有效
$path = str_replace('%3F', '?');
$path = str_replace ('%3D', '=');
Run Code Online (Sandbox Code Playgroud)
但是当我使用这些时,drupal 6会抛出错误.
我对php很新,所以它可能是我想念的简单东西
我的字符串是
$str = '<img src="\"images/hai.jpg\"" alt="" /> Text <img src="\"images/hai.jpg\"" alt="" />';
Run Code Online (Sandbox Code Playgroud)
我想从字符串中删除所有\".
字
AŠA
Run Code Online (Sandbox Code Playgroud)
PHP
<?php
foreach (glob("*.jpg") as $filename) {
$search = array("Š");
$replace = array("S");
$newname = str_replace($search, $replace, $filename);
echo $filename.'<br>'.$newname;
//($filename, realpath(dirname(__FILE__)).'/'.$newname);
}
Run Code Online (Sandbox Code Playgroud)
问题 它没有取代"AŠA"字样中的字符"Š"
我想在字符串之前和之后在字符串中添加空格,前提是以下字符不是数字(9-0).我尝试了以下代码:
newLine = re.sub(r'([,]+[^0-9])', r' \1 ', newLine)
Run Code Online (Sandbox Code Playgroud)
但似乎\1是采用2个匹配的字符而不仅仅是逗号.例:
>>> newLine = "abc,abc"
>>> newLine = re.sub(r'([,]+[^0-9])', r' \1 ', newLine)
"abc ,a bc"
Run Code Online (Sandbox Code Playgroud)
预期产出:
"abc , abc"
Run Code Online (Sandbox Code Playgroud)
我怎么能告诉他们sub只拿"逗号"?
我觉得必须有一种更有效的方法来获得相同的结果.我从SQL查询中获取数据.其中一些值返回"Y"或"N",我将其替换为"是"或"否".所以我将每个领域定位如下:
$pool= $row['POOL'];
$pool= str_replace("Y","Yes",$pool);
$pool= str_replace("N","No",$pool);
echo $pool
$wf= $row['WATERFRONT'];
$wf= str_replace("Y","Yes",$wf);
$wf= str_replace("N","No",$wf);
echo $wf
Run Code Online (Sandbox Code Playgroud)
等等...是否有一种更有效的方法将所有"Y/N"字段(POOL,WATERFRONT)组合在一起,也许是使用数组,然后可以替换值?我将如何回应每个领域?
var params = ['tom' , 'harry'];
var string = 'hello $1 and $2 how aa are you $1 and $2';
Run Code Online (Sandbox Code Playgroud)
我尝试了什么
var params = ['tom' , 'harry'];
var string = 'hello $1 ,$2 how aa are you $1 , $2';
var temp;
for(var i = 0; i<params.length ; i++)
{
temp = string.replace(/[$1]+/g,params[i]);
}
Run Code Online (Sandbox Code Playgroud)
Firefox控制台错误输出:“你好哈里,harry2哈里,你好吗,哈里2”
最终输出:你好汤姆和哈利你好吗汤姆和哈利
我有一个像这样的字符串:
var str = "/goget ? sdflfasfdsaf";
Run Code Online (Sandbox Code Playgroud)
还有一些是Half-width space和Full-width space之间/goget和sdflfasfdsaf,所以我想只有一个,以取代所有空间Half-width space
我的javascript代码是这样的:
var newstr = str.replace(/\s+/g, "");
console.log(newstr.replace("/goget","/goget "))
Run Code Online (Sandbox Code Playgroud)
但我认为这不好,我该如何改进这段代码呢?
str-replace ×10
php ×7
regex ×3
replace ×3
arrays ×2
javascript ×2
string ×2
drupal-6 ×1
node.js ×1
preg-replace ×1
python ×1
replaceall ×1