格式为“原始"在PHP中将字符串转换为Java UUID [英] Format "raw" string to Java UUID in PHP
问题描述
在PHP中,是否存在更有效,简化的方法,可以将格式"的Java UUID(不带破折号)转换为Java兼容格式(带破折号),并且最终:我将如何做?
Is there a more effective, simplified means of converting a "formatted" Java UUID - without dashes - to a Java compatible format - with dashes - in PHP, and ultimately: How would I do it?
我有已经执行此操作的代码,但似乎并不专业,我认为它可能可以更有效地完成.
I have code that already performs this action, but it seems unprofessional and I feel that it could probably be done more effectively.
[... PHP code ...]
$uuido = $json['id'];
$uuidx = array();
$uuidx[0] = substr( $uuido, 0, 8 );
$uuidx[1] = substr( $uuido, 8, 4 );
$uuidx[2] = substr( $uuido, 12, 4);
$uuidx[3] = substr( $uuido, 16, 4);
$uuidx[4] = substr( $uuido, 20, 12);
$uuid = implode( "-", $uuidx );
[... PHP code ...]
Input: f9e113324bd449809b98b0925eac3141
Output: f9e11332-4bd4-4980-9b98-b0925eac3141
$json['id']
中的数据是从以下 Mojang配置文件API 调用的使用file_get_contents( $url )
函数和json_decode( $file )
结合使用,也可以通过cURL完成-但由于最终最终一次请求最多2048个配置文件的任何内容,因此我认为它会变慢.
The data from $json['id']
is called from the following Mojang Profile API using the file_get_contents( $url )
function combined with a json_decode( $file )
, which could alternatively be done through cURL - but since it would eventually be requesting anything up to 2048 profiles at once, I figured it would become slow.
I do have my code in use, and public, through the following ProjectRogue Server Ping API, which usually contains a list of online players.
注意:有几个与此相关的问题,但据我所知,没有一个问题适用于PHP.我看过了.
Note: There are several questions related to this, but none apply to PHP as far as I am aware. I have looked.
我提到Java UUID,因为解析后的输出应有效地转换为Player UUID,以便在1.7.X之后在Spigot或Craftbukkit的基于Java的插件中使用.
I mention Java UUID as the parsed output should effectively translate to a Player UUID for use in a Java-Based plugin for Spigot or Craftbukkit after 1.7.X.
推荐答案
您的问题没有多大意义,但是假设您想以Java正确的格式读取UUID,则可以执行以下操作:
Your question doesn't make much sense but assuming you want to read the UUID in the correct format in Java you can do something like this:
import java.util.UUID;
class A
{
public static void main(String[] args){
String input = "f9e113324bd449809b98b0925eac3141";
String uuid_parse = input.replaceAll(
"(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})",
"$1-$2-$3-$4-$5");
UUID uuid = UUID.fromString(uuid_parse);
System.out.println(uuid);
}
}
从铁匠那里借来的,请参见此处: https://stackoverflow.com/a/18987428/4195825
或者在PHP中,您可以执行以下操作:
Or in PHP you can do something like:
<?php
$UUID = "f9e113324bd449809b98b0925eac3141";
$UUID = substr($UUID, 0, 8) . '-' . substr($UUID, 8, 4) . '-' . substr($UUID, 12, 4) . '-' . substr($UUID, 16, 4) . '-' . substr($UUID, 20);
echo $UUID;
?>
从fico7489借来的: https://stackoverflow.com/a/33484855/4195825
然后可以将其发送到Java,在其中可以使用fromtString()
创建UUID对象.
And then you can send that to Java where you can create a UUID object using fromtString()
.
这篇关于格式为“原始"在PHP中将字符串转换为Java UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!