468,290 Members | 2,118 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

replace part of an url with a regular expression- having trouble

I need to replace not just the ASC or DESC but the value it's sorting by. i.e. in a string like this:

http://www.mysite.com/page1.php?valu...sort=ORDER+BY+uservalue1+DESC

I've not had that much experience with regular expresions and haven't had any luck with figuring out a way to replace just the bold part in the string above (after ORDER+BY.) Bear in mind, uservalue1 and DESC after ORDER+BY may be something else, like uservalue3 ASC or uservalue 5 DESC.

I've found some great resources including the comments on the php.net function page and this board, but am still stumped.

Can anyone show me what would work and more importantly how the expression breaks down to find the string? Or, is there another way to do this that I've overlooked? Thanks!
Oct 26 '06 #1
4 3458
ronverdonk
4,258 Expert 4TB
Show the code that builds the url link+parms, then we can start with building up the parameters.

And do not forget to include the code within the appropriate [php] or [code] tags (see Posting Guidelines at the top of the forum).

Ronald :cool:
Oct 26 '06 #2
I'm grabbing the url from the browser using the code below:

[PHP]
$host = $_SERVER['HTTP_HOST'];
$self = $_SERVER['PHP_SELF'];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$url = !empty($query) ? "http://$host$self?$query" : "http://$host$self";

echo $url;[/PHP]

It is the $url value that contains the text I wish to replace.

Thank you for your help.
Oct 26 '06 #3
ronverdonk
4,258 Expert 4TB
In the following snippet I urldecoded the url string. Then I used stripos to find the ORDER BY string. Copied the url (incl. ORDER BY) and appended whatever you want to append. Then reconstructed the string urlencoding ORDER BY plus the new parameter. See if it is useable in your case. (the echoes are just for testing).
[php]<?php
$url = "http://www.mysite.com/page1.php?val...&sort=ORDER+BY+uservalue1+DESC";
echo "1. $url<br>";
$url = urldecode($url);
echo "2. $url<br>";
$pos=stripos($url, "order by");
if (!$pos)
echo "String not found";
else {
$len = $pos+1+8; // length of ORDER BY literal + 1
$pos++;
$url1 = substr($url, 0, $len) . "new sort attributes";
$url = substr($url1, 0, $pos) . urlencode(substr($url1, $pos));
echo "3. $url<br>";
}
if (!function_exists("stripos")) { // if using PHP4
function stripos($str,$needle) {
return strpos(strtolower($str),strtolower($needle));
}
}
?>
[/php]

Ronald :cool:
Oct 26 '06 #4
THANK YOU!!! I'll play with it and see how it works.

In the following snippet I urldecoded the url string. Then I used stripos to find the ORDER BY string. Copied the url (incl. ORDER BY) and appended whatever you want to append. Then reconstructed the string urlencoding ORDER BY plus the new parameter. See if it is useable in your case. (the echoes are just for testing).
[php]<?php
$url = "http://www.mysite.com/page1.php?val...&sort=ORDER+BY+uservalue1+DESC";
echo "1. $url<br>";
$url = urldecode($url);
echo "2. $url<br>";
$pos=stripos($url, "order by");
if (!$pos)
echo "String not found";
else {
$len = $pos+1+8; // length of ORDER BY literal + 1
$pos++;
$url1 = substr($url, 0, $len) . "new sort attributes";
$url = substr($url1, 0, $pos) . urlencode(substr($url1, $pos));
echo "3. $url<br>";
}
if (!function_exists("stripos")) { // if using PHP4
function stripos($str,$needle) {
return strpos(strtolower($str),strtolower($needle));
}
}
?>
[/php]

Ronald :cool:
Oct 27 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by higabe | last post: by
24 posts views Thread by Wim Roffal | last post: by
5 posts views Thread by Mahesha | last post: by
18 posts views Thread by Jon S via DotNetMonster.com | last post: by
9 posts views Thread by Whitless | last post: by
4 posts views Thread by jgabbai | last post: by
1 post views Thread by NvrBst | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.