0

I currently have a PDO script that contains a POST variable that can be either:

RU11223344992 PS:1201012 OR RU11223344992

If the value is like RU11223344992 PS:1201012 the script should interpret it by taking all values to the left of PS:1201012 leaving only RU11223344992

If the value is like RU11223344992 it should interpret it just the way it is.

Depending on this conditional, the value will be assigned to the variable PostValue.

UPDATE:

Using the suggested solution I getting the response Array when doing echo $data

$input = $_POST['postvalue'];

if (strpos($input, "PS: ")) 
{
 $data = explode($input, "PS: ");
}
else{
 $data = $input;
}
echo $data;
John
  • 965
  • 8
  • 16
  • Well it's a string, so you can use any of the [PHP String Functions](https://www.php.net/manual/en/ref.strings.php) on the `$_POST['form-input-name']` value – Martin Apr 16 '20 at 13:17
  • Can you not check the size of the string and make sure you only ever use the first 13 characters? – lukas Apr 16 '20 at 13:18
  • @lukas No it will not always be 13 characters, but it will always be anything before PS: Can you please show an example of how I can essentially trim the rest of the string value? – John Apr 16 '20 at 13:20
  • This might help along the way https://stackoverflow.com/a/4366748/2232127 – JensV Apr 16 '20 at 14:30

1 Answers1

0

Can't you use strpos to verify if there's a PS: on your input? Then you can explode it to get the first part (before space) of the string.

$input = $_POST["form-input-name"]
if (strpos($input, " PS:")){
    $data = explode(" PS:", $input)[0];
}
else{
    $data = $input;
}
vschettino
  • 101
  • 2
  • 9