简体   繁体   中英

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

#$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

Considering the user info always begins with ~$USER= and ends with a ~ we can get the result using simple 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.

However, given the string:

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

to get the $USER=james part, you can use explode as follows--

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

Now, you have the $USER info in $payNo[1]

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:

$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= ...

As previous comments said - there is no such function like substring_index in core PHP

This question is possible duplicate of following Php get string between tags topic

Here is working example with usage of strpos http://php.net/manual/pl/function.strpos.php

and 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.

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 .

what you want to do is split the inc string:

$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    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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