Question

I have the following code:

    <?php
$actual_link = "domain.com/search/q/laptop/attr/price_range_11_50005/attr/23914175_laptop";
        if ($attribute->name == "Price range")
        {
            if (strpos($actual_link,'/attr/price_range_') !== false)
                {
                    $querystring = explode("&",preg_replace(array("/attr/price_range_[0-9_]+/", "", $actual_link)));
                }
            else
                {
                    $querystring = explode("&",$actual_link);
                }
                }
        }

For some reason its not working, the code should replace the current "/attr/price_range" with nothing on the url.

Was it helpful?

Solution

Your regex is wrong:

preg_replace(array("/attr/price_range_[0-9_]+/", "", $actual_link)

As you are using the forward slash / as the delimiter, you need to escape it or use a different delimiter:

preg_replace(array("/\/attr\/price_range_[0-9_]+/", "", $actual_link)

or

preg_replace(array("#/attr/price_range_[0-9_]+#", "", $actual_link)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top