Question: I have a code like this:

http://domain.com/link.aspx?r=test5&id=3726&location=24&sublocation=

I want to know how I can delete &sublocation= and anything that maybe after it and make the code look like this:

http://domain.com/link.aspx?r=test5&id=3726&location=24

I need to use preg\_replace function for this:

So I need something like this:

```
<?php
$host = $_GET['host'];
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo preg_replace('/<link>(*.?)&sublocation=*<\/link>/', '<link> '$1' </link>', $response);

```

?>

I just need a little help to fix the preg\_replace command I have written.

Thank you

Answer: 
Just an alternative answer. It doesn't use `preg_replace`, just a `substr` and `strpos`.

```
$url  = "http://domain.com/link.aspx?r=test5&id=3726&location=24&sublocation=";
$url2 = substr($url, 0, strpos($url, '&sublocation='));
echo $url2;

```

The output will be

```
http://domain.com/link.aspx?r=test5&id=3726&location=24

```

Explanation: 

`strpos($url, '&sublocation=')` will return the position of "&sublocation=" string. Then using `substr` from 0 to that position, the original string will be cut.
Change your `*` (in `&sublocation=*`) to a `.*`. In regex, `*` means "0 or more of the preceding symbol". In your case, you have `=*`, which means "0 or more = signs".

When you use `.*`, it means "0 or more of any character (except newline)", since `.` means "any character (except newline)"\*

---

(\* unless you feed in the DOTALL flag (`s`))