472,805 Members | 1,135 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 3645
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

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

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
24
by: Wim Roffal | last post by:
Is there a possibility to do a string replace in javascript without regular experessions. It feels like using a hammer to crash an egg. Wim
5
by: Mahesha | last post by:
Hello, I need help in replacing one string pattern with another. Ex: I have a financial security expression like log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002) Here "T 3.25 6/24/2004" is a...
18
by: Jon S via DotNetMonster.com | last post by:
Hi all, I searching a string to see if "Desc" resides in it, if so then replace it with "Description". For some reason if it finds "Desc" in the word "Description" it replaces the "Desc" part...
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
9
by: Whitless | last post by:
Okay I am ready to pull what little hair I have left out. I pass the function below my String to search, my find string (a regular expression) and my replace string (another regular expression)....
4
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
4
by: jgabbai | last post by:
Hi, What is the best way to white list a set of allowable characters using regex or replace? I understand it is safer to whitelist than to blacklist, but am not sure how to go about it. Many...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.