Oscar Vaz
Oscar Vaz

Reputation: 13

PHP How to get string before special character?

I'm trying to get from this string:

UPS EXPRESSS_SAVER - 1 día hab.

the portion of: EXPRESSS_SAVER

I tried the following code: It starts from 4 index of the string and finishes before special character "-"

$shippingMethod = "UPS EXPRESSS_SAVER - 1 día hab.";
$shippingServiceId = substr($shippingMethod, 4, strpos($shippingMethod,'-'));

This returned me: EXPRESSS_SAVER - 1

I also tried :

$shippingServiceId = substr($shippingMethod, 4, strpos($shippingMethod,' '));

But got: EXP

Upvotes: 0

Views: 71

Answers (2)

Angad Choudhary
Angad Choudhary

Reputation: 14

You can use below code dot get EXPRESSS_SAVER

$shippingMethod = "UPS EXPRESSS_SAVER - 1 día hab.";

$shippingMethodArr = explode(" ",$shippingMethod);

$shipped = $shippingMethodArr[1];

Upvotes: 1

TsV
TsV

Reputation: 639

 $shippingMethod = "UPS EXPRESSS_SAVER - 1 día hab.";
 $position = 4;
 $shippingServiceId = substr($shippingMethod, $position, strpos($shippingMethod,'-') - $position);
 echo $shippingServiceId;

Upvotes: 0

Related Questions