Reputation: 5063
if we have
$id = 39827-key1-key2-key3
and we want to show only the number or anything before (-) then by using
$realid = array_shift(explode("-", $id));
we will get echo $realid; // 39827
Now my problem is as following !
if we have $id = key1/key2
and i want any way that remove the whole part key1/
and gives me only key2
how can i do it?
Upvotes: 2
Views: 69
Reputation: 8334
Using the strstr()
function, which was created exactly for things like this:
$id = 'key1/key2';
$realid = strstr($id, '/', true);
Do note that you have to be running PHP 5.3 or newer for this to work.
Upvotes: 1
Reputation: 154553
Yet another way:
$result = implode('', array_slice(explode('/', $id), 1, 1));
Upvotes: 0
Reputation: 2660
confusing question. my interpretation for $rawId
containing the '/' char:
$rawId = 'key1/key2';
$realId = substr($rawId, 1 + strpos($rawId, '/')); // key2
Upvotes: 0
Reputation: 272517
Ok, from your comment above, I'm assuming you want to do something like:
$id = "key1/key2";
$result = ???;
// Now $result=="$key2"
Why not just:
$parts = explode("/", $id);
$result = $parts[1];
Upvotes: 1