1

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.

2
  • is this a url you are creating for a link? or are you inside index.php and trying to work with the passed value in item_id? Commented Feb 16, 2012 at 16:14
  • Is this URL in a string, or the current URL (in the address bar)? Commented Feb 16, 2012 at 16:14

3 Answers 3

4
$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";
Sign up to request clarification or add additional context in comments.

1 Comment

whoa thats it, this is what I wanted. Thanks
0

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);

4 Comments

I think he wants to replace the 292 (item_id's value).
This doesn't replace the value, but the item itself.
yeah actually I wanted to replace the value, Rockets workaround seems to be perfect.
ah ok, I should have looked past the blue "Itemid" and realized it was actually the value you wanted to replace. :)
0

*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;
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.