本文整理汇总了PHP中xml_to_array函数的典型用法代码示例。如果您正苦于以下问题:PHP xml_to_array函数的具体用法?PHP xml_to_array怎么用?PHP xml_to_array使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了xml_to_array函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: xml_to_array
/**
* XML转成数组 (XML to array)
* @param string $xml XML字符串
* @return array
*/
function xml_to_array($xml)
{
if (stripos($xml, '<?xml') !== false) {
$xml = preg_replace('/<\\?xml.*?\\?>/is', '', $xml);
}
$result = array();
$pattern = '/<\\s*(.*?)(\\s+.*?)?>(.*?)<\\s*\\/\\s*\\1\\s*>/is';
preg_match_all($pattern, $xml, $matches);
if (!empty($matches[3][0])) {
foreach ($matches[3] as $key => $value) {
preg_match_all($pattern, $value, $matches_v);
if (!empty($matches_v[3][0])) {
$ret = xml_to_array($value);
} else {
$ret = $value;
}
if (array_key_exists($matches[1][$key], $result) && !empty($result[$matches[1][$key]])) {
if (is_array($result[$matches[1][$key]]) && array_key_exists(0, $result[$matches[1][$key]])) {
$result[$matches[1][$key]][] = $ret;
} else {
$result[$matches[1][$key]] = array($result[$matches[1][$key]], $ret);
}
} else {
$result[$matches[1][$key]] = $ret;
}
}
}
return $result;
}
示例2: sendSMS
public function sendSMS()
{
$post_code = isset($_POST['post_code']) ? $_POST['post_code'] : '';
$phone_num = isset($_POST['telephone']) ? $_POST['telephone'] : '';
// $post_code='U2FsdGVkX1+zY61T/9h6KxyTBWVwbNR9Z01QjZN5EmT5BzDIEROXMFb9it8VgTrW
// Yippi/B79Y0u+ZXJMwSLXGo8imoz9OTrB3k0uhvjIEyi4pF27xCm/Cg0pW0T3SoS
// 9oCORpIFF/600rCAvhDsMOADCKCBtvLhpL4YpLKHQ3/jqQFsjWF8YUVMc0x9LtPa
// 3eeGQIFsdRDr2nSWMlnGQExvNvyKnfLWUrH+YkJDIJlYzXihdv32yMw+vCf/DDa2
// Oq4CU2BkzLqff4IjGmA/9+FP2SS19kDMzdf5e1DO132QBhHDrLy1ffrSIabFRHVf
// SVDsy1qZSsC7Ea24RdmQBQ==';
if ($phone_num == '') {
return show(103, '手机号不能为空');
}
if (preg_match('/^1[34578][0-9]{9}$/', $phone_num)) {
} else {
return show(101, '手机号格式不正确');
}
$mobile_code = random(6, 1);
//random()是公共自定义函数
$target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
$post_data = "account=cf_guoqingyu&password=luping521&mobile=" . $phone_num . "&content=" . rawurlencode("您的校验码是:" . $mobile_code . "。请不要把校验码泄露给其他人。如非本人操作,可不用理会!");
//密码可以使用明文密码或使用32位MD5加密
$gets = xml_to_array(Post($post_data, $target));
if ($gets['SubmitResult']['code'] == 2) {
S('phone_num', $phone_num, 60);
S($phone_num . 'mobile_code', $mobile_code, 60);
return show(104, '发送成功');
} else {
return show(102, '发送失败');
}
}
示例3: xml_to_array
function xml_to_array($root)
{
$result = array();
if ($root->hasAttributes()) {
$attrs = $root->attributes;
foreach ($attrs as $attr) {
$result['@attributes'][$attr->name] = $attr->value;
}
}
if ($root->hasChildNodes()) {
$children = $root->childNodes;
if ($children->length == 1) {
$child = $children->item(0);
if ($child->nodeType == XML_TEXT_NODE) {
$result['_value'] = $child->nodeValue;
return count($result) == 1 ? $result['_value'] : $result;
}
}
$groups = array();
foreach ($children as $child) {
if (!isset($result[$child->nodeName])) {
$result[$child->nodeName] = xml_to_array($child);
} else {
if (!isset($groups[$child->nodeName])) {
$result[$child->nodeName] = array($result[$child->nodeName]);
$groups[$child->nodeName] = 1;
}
$result[$child->nodeName][] = xml_to_array($child);
}
}
}
return $result;
}
示例4: xml_to_array
function xml_to_array($xml)
{
$reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/";
if (preg_match_all($reg, $xml, $matches)) {
$count = count($matches[0]);
$arr = array();
for ($i = 0; $i < $count; $i++) {
$key = $matches[1][$i];
$val = xml_to_array($matches[2][$i]);
// 递归
if (array_key_exists($key, $arr)) {
if (is_array($arr[$key])) {
if (!array_key_exists(0, $arr[$key])) {
$arr[$key] = array($arr[$key]);
}
} else {
$arr[$key] = array($arr[$key]);
}
$arr[$key][] = $val;
} else {
$arr[$key] = $val;
}
}
return $arr;
} else {
return $xml;
}
}
示例5: xml_to_array
function xml_to_array($xml)
{
$fils = 0;
$tab = false;
$array = array();
foreach ($xml->children() as $key => $value) {
$child = xml_to_array($value);
//To deal with the attributes
//foreach ($node->attributes() as $ak => $av) {
// $child[$ak] = (string)$av;
//}
//Let see if the new child is not in the array
if ($tab == false && in_array($key, array_keys($array))) {
//If this element is already in the array we will create an indexed array
$tmp = $array[$key];
$array[$key] = NULL;
$array[$key][] = $tmp;
$array[$key][] = $child;
$tab = true;
} elseif ($tab == true) {
//Add an element in an existing array
$array[$key][] = $child;
} else {
//Add a simple element
$array[$key] = $child;
}
$fils++;
}
if ($fils == 0) {
return (string) $xml;
}
return $array;
}
示例6: xml_to_array
function xml_to_array($xml_object)
{
$out = array();
foreach ((array) $xml_object as $index => $node) {
$out[$index] = is_object($node) ? xml_to_array($node) : $node;
}
return (array) $out;
}
示例7: xml_to_array
protected function xml_to_array($xml_object)
{
if (defined('STRICT_TYPES') && CAMEL_CASE == '1') {
return (array) self::parameters(['xml_object' => DT::TYPE_ARRAY])->call(__FUNCTION__)->with($xml_object)->returning(DT::TYPE_ARRAY);
} else {
return (array) xml_to_array($xml_object);
}
}
示例8: discover_api
function discover_api()
{
// designed to run the built in api functions (if the exist) to get valid values for some api method calls
$idTypes = new SimpleXMLElement($this->getIdTypes());
$this->idTypes = xml_to_array($idTypes, 'idTypeList', 'idName');
$relaTypes = new SimpleXMLElement($this->getRelaTypes());
$this->relaTypes = xml_to_array($relaTypes, 'relationTypeList', 'relationType');
$sourceTypes = new SimpleXMLElement($this->getSourceTypes());
$this->sourceTypes = xml_to_array($sourceTypes, 'sourceTypeList', 'sourceName');
}
示例9: xmltoarray
function xmltoarray($xml)
{
if ($xml) {
$arr = xml_to_array($xml);
$key = array_keys($arr);
return $arr[$key[0]];
} else {
return '';
}
}
示例10: test_items
/**
* @ticket UT32
*/
function test_items()
{
$this->go_to('/feed/');
$feed = $this->do_rss2();
$xml = xml_to_array($feed);
// get all the rss -> channel -> item elements
$items = xml_find($xml, 'rss', 'channel', 'item');
$posts = get_posts('numberposts=' . $this->post_count);
// check each of the items against the known post data
for ($i = 0; $i < $this->post_count; $i++) {
// title
$title = xml_find($items[$i]['child'], 'title');
$this->assertEquals($posts[$i]->post_title, $title[0]['content']);
// link
$link = xml_find($items[$i]['child'], 'link');
$this->assertEquals(get_permalink($posts[$i]->ID), $link[0]['content']);
// comment link
$comments_link = xml_find($items[$i]['child'], 'comments');
$this->assertEquals(get_permalink($posts[$i]->ID) . '#comments', $comments_link[0]['content']);
// pub date
$pubdate = xml_find($items[$i]['child'], 'pubDate');
$this->assertEquals(strtotime($posts[$i]->post_date), strtotime($pubdate[0]['content']));
// author
$creator = xml_find($items[$i]['child'], 'dc:creator');
$this->assertEquals($this->author->user_nicename, $creator[0]['content']);
// categories (perhaps multiple)
$categories = xml_find($items[$i]['child'], 'category');
$cat_ids = wp_get_post_categories($post->ID);
if (empty($cat_ids)) {
$cat_ids = array(1);
}
// should be the same number of categories
$this->assertEquals(count($cat_ids), count($categories));
// ..with the same names
for ($j = 0; $j < count($cat_ids); $j++) {
$this->assertEquals(get_cat_name($cat_ids[$j]), $categories[$j]['content']);
}
// GUID
$guid = xml_find($items[$i]['child'], 'guid');
$this->assertEquals('false', $guid[0]['attributes']['isPermaLink']);
$this->assertEquals($posts[$i]->guid, $guid[0]['content']);
// description/excerpt
$description = xml_find($items[$i]['child'], 'description');
$this->assertEquals(trim($posts[$i]->post_excerpt), trim($description[0]['content']));
// post content
if (!$this->excerpt_only) {
$content = xml_find($items[$i]['child'], 'content:encoded');
$this->assertEquals(trim(apply_filters('the_content', $posts[$i]->post_content)), trim($content[0]['content']));
}
// comment rss
$comment_rss = xml_find($items[$i]['child'], 'wfw:commentRss');
$this->assertEquals(html_entity_decode(get_post_comments_feed_link($posts[$i]->ID)), $comment_rss[0]['content']);
}
}
示例11: testToArray
public function testToArray()
{
$content = '<foo>
<bar hello="hello world">
Hello
</bar>
</foo>';
$array_data = xml_to_array($content);
$this->assertTrue(is_array($array_data));
// Array
//(
// [0] => Array
// (
// [tag] => FOO
// [type] => open
// [level] => 1
// [value] =>
//
// )
//
// [1] => Array
// (
// [tag] => BAR
// [type] => complete
// [level] => 2
// [attributes] => Array
// (
// [HELLO] => hello world
// )
//
// [value] => Hello
// )
//
// [2] => Array
// (
// [tag] => FOO
// [value] =>
// [type] => cdata
// [level] => 1
// )
//
// [3] => Array
// (
// [tag] => FOO
// [type] => close
// [level] => 1
// )
//
// )
//)
}
示例12: get_xml
function get_xml($id)
{
$folder = $this->dir_root . "plugins/" . $id . "/";
if (!is_dir($folder)) {
return false;
}
$rs = array();
if (is_file($folder . "config.xml")) {
$rs = xml_to_array(file_get_contents($folder . "config.xml"));
}
$rs["id"] = $id;
$rs["path"] = $folder;
return $rs;
}
示例13: send_sms
function send_sms($phoneNumber, $message)
{
global $smsUserName, $smsPassword;
$target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
$userName = $smsUserName;
$passWord = $smsPassword;
$post_data = "account={$userName}&password={$passWord}&mobile={$phoneNumber}&content=" . rawurlencode($message);
$gets = xml_to_array(send_server($post_data, $target));
$ret = "";
if ($gets['SubmitResult']['code'] == 2) {
return true;
} else {
printResultByMessage($gets['SubmitResult']['msg'], 109);
}
}
示例14: actionMonitorTask
public function actionMonitorTask($task_id)
{
$task = DcmdTask::findOne($task_id);
///非系统管理员只能操作同一产品组的该任务
if (Yii::$app->user->getIdentity()->admin != 1) {
if ($task->opr_uid != Yii::$app->user->getId()) {
///判断是否为同一产品组
$app = DcmdApp::findOne($task->app_id);
$query = DcmdUserGroup::findOne(['uid' => Yii::$app->user->getId(), 'gid' => $app['svr_gid']]);
if ($query == NULL) {
Yii::$app->getSession()->setFlash('success', NULL);
Yii::$app->getSession()->setFlash('error', "对不起,你没有权限!");
return $this->redirect(array('dcmd-task/index'));
}
}
} else {
///系统管理员产品所属同一系统组
$app = DcmdApp::findOne($task->app_id);
$query = DcmdUserGroup::findOne(['uid' => Yii::$app->user->getId(), 'gid' => $app['sa_gid']]);
if ($query == NULL) {
Yii::$app->getSession()->setFlash('success', NULL);
Yii::$app->getSession()->setFlash('error', "对不起,你没有权限!");
return $this->redirect(array('dcmd-task/index'));
}
}
if (Yii::$app->request->post()) {
$timeout = Yii::$app->request->post()['timeout'];
$auto = Yii::$app->request->post()['auto'];
$concurrent_rate = Yii::$app->request->post()['concurrent_rate'];
$this->updateTask($task_id, $timeout, $auto, $concurrent_rate);
////var_dump(Yii::$app->request->post());exit;
}
///获取产品名称
///$query = DcmdService::findOne($task->svr_id);
$app_id = $task->app_id;
/// $query->app_id;
///$query = DcmdApp::findOne($app_id);
$app_name = $task->app_name;
$ret = xml_to_array($task->task_arg);
$args = "";
if (is_array($ret['env'])) {
foreach ($ret['env'] as $k => $v) {
$args .= $k . '=' . $v . " ; ";
}
}
return $this->render('monitor', ['task_id' => $task_id, 'task' => $task, 'args' => $args, 'app_name' => $app_name, 'app_id' => $app_id]);
}
示例15: xml_to_array
public static function xml_to_array($xml)
{
$reg = "/<(\\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
if (preg_match_all($reg, $xml, $matches)) {
$count = count($matches[0]);
for ($i = 0; $i < $count; $i++) {
$subxml = $matches[2][$i];
$key = $matches[1][$i];
if (preg_match($reg, $subxml)) {
$arr[$key] = xml_to_array($subxml);
} else {
$arr[$key] = $subxml;
}
}
}
return $arr;
}