user393273
user393273

Reputation: 1438

get text after the _ in a string

basically i just want to get all the text after the _ .

i have tried

$productid = split("_",$PagePath, 1);

with no success what is the correct way of doing this?

Upvotes: 0

Views: 425

Answers (4)

Fabrizio D'Ammassa
Fabrizio D'Ammassa

Reputation: 4769

You could try:

$productidtokens = explode("_",$pagePath, 2);
if(count($productidtokens)>1)
  $productid = $productidtokens[1];

Upvotes: 2

oreX
oreX

Reputation: 72

$productId = substr($string, (strpos($PagePath, '_') + 1)); //+1 accounts for the underscore

Upvotes: 1

Habib Rosyad
Habib Rosyad

Reputation: 342

Try $productid = explode('_',$PagePath).

If you just intended to take the text after the '_', then get rid the first index in the array created by write : array_shift($productid).

Upvotes: 0

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124768

Use explode instead of split. The result of explode is an array, so use list():

list(,$productid) = explode('_', $PagePath, 2);

Note the third parameter, 2 instead of 1. Using 1 will not split anything. Or, just use preg_replace:

$productid = preg_replace('/^.*?_/', '', $PagePath);

Upvotes: 3

Related Questions