简体   繁体   English

数组和子字符串php

[英]Array and substring php

     //array data                          
   {
    $results[] = $result;

    $SiteID=$result["site_id"];
    $pay_sale_id=$result["pay_sale_id"];
    $pay_payment_info=$result["pay_payment_info"];
    $payNo= substring_index(substring_index('$result["pay_payment_info"]', '$USER=', -1), '~$TAGROLE', 1);

 }

The content of pay_payment_info is as follows pay_payment_info的内容如下

#$TAG=6F0000173~$USER=james~$TAGROLE=0

I want to extract only the user info, but i get error: 我只想提取用户信息,但出现错误:

Fatal error: Call to undefined function substring_index() in line 致命错误:调用未定义函数substring_index()

Considering the user info always begins with ~$USER= and ends with a ~ we can get the result using simple regex: 考虑到用户的信息总是开头~$USER=和一个结束~我们可以使用简单的regex得到的结果:

preg_match('/\~\$USER=(?<user>[^~]+)/', $pay_payment_info, $match);
var_dump($match['user']);

The error tells you exactly why it is an error: substring_index is not a valid/native PHP function... you can define your own function of substring_index , though. 该错误告诉您确切的原因: substring_index不是有效/本机的PHP函数...不过,您可以定义自己的substring_index函数。

However, given the string: 但是,给定字符串:

$TAG=6F0000173~$USER=james~$TAGROLE=0 $ TAG = 6F0000173〜$ USER =詹姆斯〜$ TAGROLE = 0

to get the $USER=james part, you can use explode as follows-- 要获得$ USER = james部分,可以使用explode ,如下所示:

$payNo = explode("~", $result["pay_payment_info"])

Now, you have the $USER info in $payNo[1] 现在,您在$payNo[1]$USER信息。

If you want to go even further and just get the value of what $USER value is, then you can use PHP's native substr function: 如果您想走得更远并且获得$USER值的值,则可以使用PHP的本机substr函数:

$justUserPart = substr($payNo[1], strpos($payNo[1], "=")+1);
echo $justUserPart;

Please Note : The above assumes that you will always have the string in the format of 请注意 :以上假设您将始终使用以下格式的字符串

$TAG= ... ~$USER= ... ~$TAGROLE= ... $ TAG = ... 〜$ USER = ... 〜$ TAGROLE = ...

As previous comments said - there is no such function like substring_index in core PHP 如之前的评论所述-核心PHP中没有像substring_index这样的函数

This question is possible duplicate of following Php get string between tags topic 这个问题可能是以下Php在标签主题之间获取字符串的重复

Here is working example with usage of strpos http://php.net/manual/pl/function.strpos.php 这是使用strpos http://php.net/manual/pl/function.strpos.php的工作示例

and substr http://php.net/manual/pl/function.substr.php substr http://php.net/manual/pl/function.substr.php

$var = '$TAG=6F0000173~$USER=james~$TAGROLE=0';
$m = substr($var, strpos($var, '~$USER=')+7);
var_dump($m); //returns string(16) "james~$TAGROLE=0"
$m = substr($m, 0, strpos($m, '~$'));
var_dump($m); //returns string(5) "james"

it seems that the problem is you have no substring_index() function or method, yet you attempt to invoke it twice. 似乎问题是您没有substring_index()函数或方法,但尝试将其调用两次。

I am not sure why you use the ~ as a delimiter but that should be ok, although using a , would have resulted in less work; 我不知道为什么你使用~作为分隔符,但应该是好的,但使用,将导致更少的工作; each of the attributes of your querystring would have been able to be addressed directly then. 这样,您就可以直接解决查询字符串的每个属性。

what you want to do is use the php version of split() which is explode() here is a link to the manual . 您想要做的是使用split()php版本,即explode()这是手册链接

what you want to do is split the inc string: 您要做的是分割inc字符串:

$aUserInfo = explode('~', $result['pay_payment_info']);

now you have an array. 现在您有了一个数组。 loop through it and make it like you want: 遍历它,并使其如您所愿:

$result = array();
foreach($aUserInfo as $v){
  $aTmp = explode('=',$v);
  $result[$aTmp[0]] = $aTmp[1];
}

at the end of this you have an array with keys as keys and their respective values as values, ie 最后,您将得到一个数组,其中包含键作为键,其各自的值作为值,即

$result = array( 'tag' => '6F0000173',
                 'user'=> 'James',
                 'tagrole'=> 0    );

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

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