我有一个问题,我想在动态字符串中获取值,这太复杂了。这是字符串
<['pritesh:name:nilesh:replace']>这是一个动态字符串,我想在这个字符串中获取名称和替换变量值。
发布于 2012-11-28 22:03:33
我不太确定您的字符串的格式,但这里有一些东西可以帮助您。
您可以使用explode将带有分隔符的字符串转换为数组。然后,您可以更改一个值并将其转换回以":“分隔的形式。您可以使用implode的别名join来执行此操作
<?php
// initialize variable and print it
$s = "pritesh:name:nilesh:replace";
print("{$s}\n");
$s = explode(":", $s); // convert to array
$s[1] = "anotherName"; // change value
// convert back to foo:bar form and print
$s = join($s, ":");
print("{$s}\n");
?>将其放入文件example.php并在命令行上运行:
$ php -q example.php
pritesh:name:nilesh:replace
pritesh:anotherName:nilesh:replace正如有人提到的,如果您需要处理更高级的格式,您应该学习如何使用regular expressions in PHP。
希望这能有所帮助!
发布于 2012-11-28 21:59:54
$exploded = explode(':', $string);
$exploded[1] = $replacement;
$string = implode(':', $exploded);发布于 2012-11-28 22:03:11
假设字符串存储在名为$string的变量中,则:
$parts = explode(':', $string);
// this will mean that
// $parts[0] contains pritesh, $parts[1] = name, $parts[2] = nilesh and $parts[3] = replace
// therefore
$name = $parts[0];
$replace = $parts[2];https://stackoverflow.com/questions/13606759
复制相似问题