Reputation: 18016
I have a URL string like
http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9
I want to remove my_link_id:1
from this string. I know I can use any string replace function like
$goodUrl = str_replace('my_link_id:1', '', $badUrl);
But the problem is, the integer part is dynamic. I mean in my_link_id:1
the 1
is dynamic. It is the ID of the link and it can be from 0 to any number
. So I want to remove my_link_id:along with any dynamic number from the string
.
What I think I should remove part of the string from last /
to #
. But how can I do that?
Upvotes: 0
Views: 111
Reputation: 219
May following help you
$strUrl = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
$finalUrl = preg_replace('/(my_link_id:[0-9]+)/i', '', $strUrl);
echo $finalUrl;
and If you want remove also last / and # you follow this code
$strUrl = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
$finalUrl = preg_replace('/(\/my_link_id:[0-9]+#)/i', '', $strUrl);
echo $finalUrl;
Upvotes: 0
Reputation: 1560
You can use preg_replace() php function
http://in1.php.net/preg_replace
CODE:
<?php $string = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
echo $result = preg_replace('/(my_link_id:[0-9]+)/si', '', $string);
?>
Output :
http://mydomain.com/status/statusPages/state/stack:3/#state-9
Upvotes: 0
Reputation: 24229
you can use regular expressions:
$goodUrl = preg_replace('/(my_link_id:[0-9]+)/ig', '', $badUrl);
Upvotes: 1