sam orten
sam orten

Reputation: 206

How to remove before portion of string in php ?

$keyword = "ios-developer-jobs-in-noida";
$city = strstr($keyword, 'in-');
echo $city;

I have a string in my URL now I want to remove ios-developer-jobs-in- but using strstr($keyword, 'in-'); it shows in-noida. I want only noida. So, How can I resolve this issue? Please help me.

Thank You

Upvotes: 1

Views: 34

Answers (5)

mickmackusa
mickmackusa

Reputation: 47991

We don't know how much your string may vary or if -in- is guaranteed to exist, but this will work on your sample input.

$keyword = "ios-developer-jobs-in-noida";

echo explode('-in-', $keyword, 2)[1];

Use -in- as your delimiter and cap the explosion at 2 elements. Use the 2nd element.

Upvotes: 0

Roshan Patil
Roshan Patil

Reputation: 196

You can use explode method Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

$keyword = "ios-developer-jobs-in-noida";
$city = explode("-",substr($keyword, strpos($keyword,'in-')));
echo $city[1];

Upvotes: 0

Shibon
Shibon

Reputation: 1574

Try like this if character comes after last -

substr($foo, (strrpos($foo, '-', -1))+1);

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133380

Ypu could use strrpos and substr

$keyword = "ios-developer-jobs-in-noida";

$city =   substr(str, (-1)*strrpos($keyword, "-"))

Or explode and end()

$myArray = explode("-",$keyword);
$city =  end($myArray);

Upvotes: 0

sradha
sradha

Reputation: 2244

Try this one

<?php
$keyword = "ios-developer-jobs-in-noida";
$city = explode("-",substr($keyword, strpos($keyword,'in-')));
echo $city[1];
?>

Upvotes: 1

Related Questions