-1
     //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

4
  • have you defined function substring_index Commented Jan 8, 2018 at 16:21
  • 2
    There is no substring_index() function in PHP. Commented Jan 8, 2018 at 16:22
  • Try this strpos Commented Jan 8, 2018 at 16:24
  • 2
    You are mixing MySQL code with PHP code... those are totally different languages. Commented Jan 8, 2018 at 16:26

4 Answers 4

1

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']);
Sign up to request clarification or add additional context in comments.

Comments

0

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"

1 Comment

Note: the above will fail, if OP decides to name $USER to say, $USERINFO or $USERNAME etc. (i.e. anything >7 chars)
0

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, i.e.

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

4 Comments

how about if use substring in sql like this $sql="INSERT INTO payments_data(site_id, Pay_sale_id, Pay_paymnet_type, Pay_payment_info) VALUES ('00','0','0', substring_index(substring_index('$pay_payment_info', '$USER=', -1), '~$TAGROLE', 1) )";
what is this substring_index() you keep wanting to invoke? it is not a built in function. I am not sure if you are attempting to concatenate a string and store it, but that seems the most likely. ... you should restructure your data, but if that isnt possible, spit the string into there, leave out that other method, or let me know what it is supposed to do and I can likely emulate it
I am trying to store only a portion of data, I want to extract user name and store it in a database
ahh ok, $result["user"] should be that value
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.