PHP Replace Variables in URL

Hello,

I wondered if it was possible to rewrite a URL so designed for the rewrite wrote in htaccess

e.g.
I have the URL: $next_url = “$your_search_page?s=$news&kw=$append”;

The URL goes here: http://example.com/?s=20&kw=Garden/Center/Product

But I want to change the URL to: http://example.com/Garden/Center/Product/P2.html

This is just for the href, the htaccess bit is solved.

Hmm, what about…


<?php
function funkify_url($url){
  list($host, $qs) = explode('?', $url);
  parse_str($qs, $params);
  return sprintf(
    'http://%s/%s/P%s.html',
    parse_url($host, PHP_URL_HOST),
    $params['kw'],
    $params['s']
  );
}

var_dump(
  funkify_url('http://www.example.org?s=20&kw=Garden/Center/Product')
);

/*
  string(53) "http://www.example.org/Garden/Center/Product/P20.html"
*/
?>

Hi AnthonySterling,

I have no idea what you have wrote here:) but I pasted this (below) on my page. When I click next (thats the link) it takes me to: http://example.com/[COLOR=“Red”]?s=100&kw=[/COLOR]Garden/Center/Product/P2

I’m not sure if I’m adding this correctly. Also on my page are other ‘%s’ symbols, I’m not sure if they interfer with each other. But I do notice ALL the links are going to: http://example.com/[COLOR=“Red”]?s=100&kw=[/COLOR]Garden/Center/Product/P2 …except the “red” variables change.


function funkify_url($url){
  list($host, $qs) = explode('?', $url);
  parse_str($qs, $params);
  return sprintf(
    'http://%s/%s/P%s.html',
    parse_url($host, PHP_URL_HOST),
    $params['kw'],
    $params['s']
  );
}

Here is a look at a snipped of my code:


if ($current_page != $pages) {
$finals = ($pages - 1) * $limit;
$url = "$your_search_page?s=$finals&amp;kw=$append";
			if ($sort_enabled == TRUE && isset($_GET['o'])) {
			$url .= "&amp;o=$o";
			}
echo "<a href=\\"$url\\">$last_text</a>";
}
}
// check to see if last page
	if (!((($s+$limit)/$limit)==$pages)) {
// not last page so give NEXT link 
// new s value = s + number of rows per page

function funkify_url($url){
  list($host, $qs) = explode('?', $url);
  parse_str($qs, $params);
  return sprintf(
    'http://%s/%s/P%s.html',
    parse_url($host, PHP_URL_HOST),
    $params['kw'],
    $params['s']
  );
}


  $news=$s+$limit;
	$next_url = "$your_search_page?s=$news&amp;kw=$append";
	if ($sort_enabled == TRUE && isset($_GET['o'])) {
	$next_url .= "&amp;o=$o";
	}
	echo " | <a href=\\"$next_url\\">$next_text</a>\
";
  } 
echo "</div></center>";
}
}

Thanks

I’ve been playing with your function, dose is matter about being a subdomain?

Hi Anthony,

I’m still working on these paging URLs! I finally figured out how to connect the function to the variable. That was a big help:) I like this little function. The only trouble I’m having is the way I have it set up (below) it creates a URL like this:
http://example.com/-P10.html

It dosen’t add the $news variable to the URL. I’ve worked with it very much and can’t see anything wrong. Does it appear to you that I have it set up correct? I changed the slashes to dashes, they look better to me.

Thanks

function funkify_url($url){
  list($host, $qs) = explode('?', $url);
  parse_str($qs, $params);
  return sprintf('http://%s/%s-P%s.html', parse_url($host, PHP_URL_HOST), $params['kw'], $params['s']);
}


      if (!((($s+$limit)/$limit)==$pages)) {
  $news=$s+$limit;
	
	$next_url = funkify_url("$your_search_page?s=$news&amp;kw=$append");
	if ($sort_enabled == TRUE && isset($_GET['o'])) {
	$next_url .= "&amp;o=$o";
	}
	echo " | <a href=\\"$next_url\\">$next_text</a>\
";