Reputation: 1034
I have the following url
/index.php?option=com_zoo&task=item&item_id=292&Itemid=283
What I want to do to replace item_id
's value with a variable. I have been checking a couple of php functions like split
and parse_str
, but I do not know how I to get it to work.
Upvotes: 1
Views: 2661
Reputation: 1770
*this is the exact way of doing it *
<?php
$url = '/index.php?option=com_zoo&task=item&item_id=292&Itemid=283';
$explodeData =parse_url($url);
$dataReplace = str_replace('item_id','replacedvariable',$explodeData['query'],$count);
$changedUrl = $explodeData['path']."?".$dataReplace;
echo $changedUrl;
?>
Upvotes: 0
Reputation: 542
Try the str_replace
function. If you have your URL stored in the variable $url
and the variable you want to replace Itemid stored in $ItemID
:
$url = str_replace("Itemid", $ItemID, $url);
Upvotes: 0
Reputation: 227240
$url = '/index.php?option=com_zoo&task=item&item_id=292&Itemid=283';
$query = explode('?', $url); // Split the URL on `?` to get the query string
parse_str($query[1], $data); // Parse the query string into an array
echo $data['item_id']; // 292
$newValue = 300;
$data['item_id'] = $newValue; // Replace item_id's value
$url = $query[0].'?'.http_build_query($data); // rebuild URL
echo $url; // '/index.php?option=com_zoo&task=item&item_id=300&Itemid=283";
Upvotes: 4