[英]Change a string using str_replace() Function with PHP
How can I change this: 我该如何更改:
[ "Aluno1": 65.8, "Aluno2": 0.3, "Aluno3": 34.9 ]
to have this: 有这个:
['dafa', 43.00],['adfasdf', 13.00],['cu', 0.00]
maybe changing :
to ,
and ,
to ][
, but how can I do that using str_replace()
? 可能会更改:
,
和,
][
,但是如何使用str_replace()
做到这一点?
To use str_replace()
you can wrap what you want to change in % and then apply. 要使用str_replace()
您可以将要更改的内容包装在%中,然后应用。 Example below; 下面的例子;
$old_sentence = "My name is %name%";
$new_sentence = str_replace('%name%', 'ADAM', $old_sentence);
echo $new_sentence;
"My name is ADAM" “我的名字是ADAM”
Hope this gives you an idea of how to use it. 希望这会让您对如何使用它有所了解。
looks like you need json_decode($array); 看起来你需要json_decode($ array);
so like: 像这样:
$your_array=[ "Aluno1": 65.8, "Aluno2": 0.3, "Aluno3": 34.9 ];
json_decode($your_array);
But json is so nice to have arrays in why convert it back? 但是json非常适合拥有数组,为什么还要将其转换回?
Here is the answer. 这是答案。 In fact it's more complicated that simply replace ":" to "," for example. 实际上,简单地将“:”替换为“,”会更加复杂。 Try this : 尝试这个 :
$Stringtoreplace = '[ "Aluno1": 65.8, "Aluno2": 0.3, "Aluno3": 34.9 ]';
$NewStringReplace = str_replace('":','",',$Stringtoreplace);
$NewStringReplace = str_replace(', "','],["',$NewStringReplace);
//If you want to change every double quotes to single remove line below
//$NewStringReplace = str_replace('"',"'",$NewStringReplace);
echo $NewStringReplace;
// echo : [ "Aluno1", 65.8],["Aluno2", 0.3],["Aluno3", 34.9 ]
Ok, thank you guys. 好的,谢谢。 I got it by using the following code: 我通过使用以下代码得到它:
$old_sentence = "[ "Aluno1": 65.8, "Aluno2": 0.3, "Aluno3": 34.9 ]";
$new_sentence = str_replace(',', '],[', $old_sentence);
$output = str_replace(':', ',', $new_sentence);
echo $output;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.