简体   繁体   English

在PHP端解析字符串的更好方法?

[英]Better way to parse a string on PHP side?

I'm passing a javascript variable in my PHP form that gets passed as formatted below: 我正在以我的PHP形式传递一个javascript变量,该变量的格式如下:

<form onsubmit='$("#crop_cords").val($.param(c).toString()); return true;' id="process_image_form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

crop_cords='x=50&y=50&x2=400&y2=400&w=350&h=350'

Is there a better way to parse the value for crop_cords on the server side or do I need to use split and/or explode? 是否有更好的方法可以在服务器端解析crop_cords的值,还是需要使用split和/或explode? I was hoping to find a cleaner solution. 我希望找到一个更清洁的解决方案。 The result should look like this: 结果应如下所示:

x=50
y=50
x2=400
y2=400
w=350
h=350

You are looking for parse_str . 您正在寻找parse_str

$crop_cords='x=50&y=50&x2=400&y2=400&w=350&h=350';
parse_str($crop_cords, $parsed);

print_r($parsed);

See it in action . 看到它在行动

You can use parse_str to parse the string as if it were the querystring: 您可以使用parse_str来解析字符串,就好像它是查询字符串一样:

$params = array();
parse_str($_POST['crop_cords'], $params);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM