PHP Get URL without Query String - While Modifiying existing query - php

Long time lurker first time poster here:
I have searched high and low and am trying to keep my php script somewhat the same as it is:
$url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
however when I echo $url I need to:
Remove the string from www.example.com/?utm_source=etc
removing everything after /? on multiple file names like
www.example.com/page.htm/?utm_source=etc
www.example.com/page1.htm/?utm_source=etc
and so on
Keep the google custom search query string www.example.com/search.htm?q=term
Keep a couple other search string close the GSE string example
Ive seen some examples but none worked for me without making a ton of changes, surley there is a easier way.
Thanks in Advance

This will do the job for you. I used php regex and preg_replace() predefined function for it.
Regex (Fiddle Link)
/www(\.)[A-Za-z0-9]+\.\w+(\/)(\?)?((\w)+(\.)(\w)+)?((.)+)?/i
Php example
<?php
$input_line = 'www.example.com/?utm_source=etc'; //Input String
$replace_String = ''; //specify your replace string here
$regex = preg_replace("/www(\.)[A-Za-z0-9]+\.\w+(\/)(\?)?((\w)+(\.)(\w)+)?((.)+)?/i", $replace_String, $input_line);
print_r($regex); //this will print the output with replaced string
?>

You need to split url into different pieces and then put all of it back together - this is the only way.
So for example if your url is
$url = www.example.com/page1.htm/?utm_source=etc&some_key=some_key_value&someothekey=someotherid
$url_array = explode("?",$url);
$url_path = $url_array[0];
$url_params = explode("&",$url_array[1]);
$i = 0;
foreach($url_params as $url_param){ // Let's get specific url parameter and it's value
$i++;
$url_key = explode("=",$url_param)[0];
$url_value = explode("=",$url_param)[1];
$some_key = "some_key";
/*
* or use $i auto increment to find, relevant search parameter based on position e.g.
* if($i = 0){ // 0 is first etc.
* $some_key = $url_key;
* }
*/
// now that we have all keys - let's compare
if($url_key === $some_key){
$some_key_value .= $url_value; // Note the dot before ".=" so you can use it outside the loop, do the same if statements for any other keys you need
}
}
$new_url = $url_path."?".$some_key."=".$some_key_value;

Related

very large php string magically turns into array

I am getting an "Array to string conversion error on PHP";
I am using the "variable" (that should be a string) as the third parameter to str_replace. So in summary (very simplified version of whats going on):
$str = "very long string";
str_replace("tag", $some_other_array, $str);
$str is throwing the error, and I have been trying to fix it all day, the thing I have tried is:
if(is_array($str)) die("its somehow an array");
serialize($str); //inserted this before str_replace call.
I have spent all day on it, and no its not something stupid like variables around the wrong way - it is something bizarre. I have even dumped it to a file and its a string.
My hypothesis:
The string is too long and php can't deal with it, turns into an array.
The $str value in this case is nested and called recursively, the general flow could be explained like this:
--code
//pass by reference
function the_function ($something, &$OFFENDING_VAR, $something_else) {
while(preg_match($something, $OFFENDING_VAR)) {
$OFFENDING_VAR = str_replace($x, y, $OFFENDING_VAR); // this is the error
}
}
So it may be something strange due to str_replace, but that would mean that at some point str_replace would have to return an array.
Please help me work this out, its very confusing and I have wasted a day on it.
---- ORIGINAL FUNCTION CODE -----
//This function gets called with multiple different "Target Variables" Target is the subject
//line, from and body of the email filled with << tags >> so the str_replace function knows
//where to replace them
function perform_replacements($replacements, &$target, $clean = TRUE,
$start_tag = '<<', $end_tag = '>>', $max_substitutions = 5) {
# Construct separate tag and replacement value arrays for use in the substitution loop.
$tags = array();
$replacement_values = array();
foreach ($replacements as $tag_text => $replacement_value) {
$tags[] = $start_tag . $tag_text . $end_tag;
$replacement_values[] = $replacement_value;
}
# TODO: this badly needs refactoring
# TODO: auto upgrade <<foo>> to <<foo_html>> if foo_html exists and acting on html template
# Construct a regular expression for use in scanning for tags.
$tag_match = '/' . preg_quote($start_tag) . '\w+' . preg_quote($end_tag) . '/';
# Perform the substitution until all valid tags are replaced, or the maximum substitutions
# limit is reached.
$substitution_count = 0;
while (preg_match ($tag_match, $target) && ($substitution_count++ < $max_substitutions)) {
$target = serialize($target);
$temp = str_replace($tags,
$replacement_values,
$target); //This is the line that is failing.
unset($target);
$target = $temp;
}
if ($clean) {
# Clean up any unused search values.
$target = preg_replace($tag_match, '', $target);
}
}
How do you know $str is the problem and not $some_other_array?
From the manual:
If search and replace are arrays, then str_replace() takes a value
from each array and uses them to search and replace on subject. If
replace has fewer values than search, then an empty string is used for
the rest of replacement values. If search is an array and replace is a
string, then this replacement string is used for every value of
search. The converse would not make sense, though.
The second parameter can only be an array if the first one is as well.

Trim a url to just the domain name using PHP

I have a database table column that stores urls of a persons website. This column is unique as I don't want people using the same website twice!
However a person could get around this by doing:
domain.com
domain.com/hello123
www.domain.com
So my plan is to make it so that when a person saves their record it will remove everything after the first slash to make sure only the domain is saved into the database.
How would I do this though? I'm presuming this has been done lots of times before, but I'm looking for something VERY VERY simple and not interested in using libraries or other long code snippets. Just something that strips out the rest and keeps just the domain name.
See PHP: parse_url
// Force URL to begin with "http://" or "https://" so 'parse_url' works
$url = preg_replace('/^(?!https?:\/\/)(.*:\/\/)/i', 'http://', $inputURL);
$parts = parse_url($url);
// var_dump($parts); // To see the parsed URL parts, uncomment this line
print $parts['host'];
Note, the subdomains are not unique using the code as listed. www.domain.com and domain.com will be separate entries.
Use parse_url:
$hostname = parse_url($userwebsite,PHP_URL_HOST);
$sDomain = NULL;
foreach (explode('/', $sInput) as $sPart) {
switch ($sPart) {
case 'http:':
case 'https:':
case '':
break;
default:
$sDomain = $sPart;
break 2;
}
}
if ($sDomain !== NULL) {
echo $sDomain;
}
First, all slashes are used as separators. Next, all "known/supported" schemes are ignored, as well as the empty part which happens from "http://". Finally, whatever is next will be stored in $sDomain.
If you do not mind the dependency of PCRE, you can use a regular expression as well:
if (preg_match('/^https?:\/\/([^\/]+)/', $sInput, $aisMatch) === 1) {
echo $aisMatch[1];
}
You could try
int strrpos ( string $haystack , string $needle [, int $offset = 0 ] )
and then put the result of that into
string substr ( string $string , int $start [, int $length ] )
using $needle = "/" and $needle = "."

replace only part of string witg preg_replace

I have little problem with a replacement of a little part in an url.
I have an url with some queries that I build with http_build_query().
In that url i have a query like angle_30. What I want is to remove the _30 so the query becomes angle.
How can I do that with preg_replace?
I have created a search and replacement array
$search_pattern = array();
$search_pattern[0] = "/([?&]newpage)=[^&]*/";
$search_pattern[1] = "/([?&]next)=[^&]*/";
$search_replacements = array();
$search_replacements[0] = '';
$search_replacements[1] = '';
which works fine. I just want to extend it with the angle replacement.
Here is a search and replacement pattern that replaces "angle_", followed by an arbitrary number of digits with "angle".
$search_pattern[2] = "/angle_\d+/";
$search_replacements[2] = "angle";

how to parse this url?

if I have this url: node/95/pdf/1. How will I able to get the numeric/value 1? Tried the parse_url but gave me the wrong output.
PS: the value 1 is just an example, the id is dynamic depends on what the user click.
I would use sscanf
Untested example:
list($node_id, $pdf_id) = sscanf($url, "node/%d/pdf/%d");
$node_id contains the node id, $pdf_id contains the pdf id. According to your comment: Yes, you can output it with e.g. echo $pdf_id;.
If you need them both in an array, you can remove the list() method, doing it like this:
$ids = sscanf($url, "node/%d/pdf/%d");.
This returns an array with both node and pdf id in $ids.
Finally, if you just need the pdf id, you could do
$id = sscanf($url, "node/95/pdf/%d");.
I just showed how to fetch both because I assumed you may need both numbers from your url.
Edit
seeing all the other answers after posting my solution, I am wondering why everyone is solving this with multiple functions when there is a function available that does exactly what he needs: parsing a string according to a format. This also leads to less sql-injection prone code IMHO. And it doesn't break something when the url gets extended or query strings are appended.
Edit 2
list($node_id, $sub, $sub_id) = sscanf($url, "node/%d/%[^/]/%d"); will get you the "pdf" and it's id separate instead of "node/%d/%s/%d". This is because char / is also matched by %s. Using %[^/] matches everything except the forward slash.
You can do this:
$id = end(explode('/', 'node/95/pdf/1'));
Example:
$arr = explode('/', 'node/95/pdf/1');
$id = end($arr);
echo $id; // 1
$url = "node/95/pdf/1";
// Find the last occurence of a slash, get everything after that.
$id = substr($url, strrpos($url, "/") + 1 );
Try with:
$input = 'node/95/pdf/1';
$parts = explode('/', $input);
$output = (int) $parts[3];

Passing Variables in URL - PHP

Assume that I have a URL like this
http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT1=Value
In this URL, TEXT1 at the end keeps changing for various pages. The Value will not change though. So it will be something like
For Page 1
http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT1=Value
For Page 2
http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT2=Value
For Page n
http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXTn=Value
How can I parametrize it? I tried something like this
for ($i=1;$i<=n;$i++)
{
$url = sprintf('http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT%d=Value',$i)
echo $url;
}
but it failed saying Sprintf too few arguments. Any suggestion, please?
You have more than one % sign in that url, sprintf parses it and tries to assign arguments to every %'something' it finds, you should escape the url encoded values.
You might want to check: http://www.php.net/manual/en/function.sprintf.php
Just use urldecode because the more than one (additional)% is creating problem .
$url=urldecode('http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT%d=Value');
$url = sprintf($url,$i);
$url = 'http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT'.$i.'=Value';
You can use the normal string also right, instead of using sprintf
for ($i=1 ; $i < = n ; $i++ )
{
$url = "http://subdomain.domain.com/folder1/abc?cat1=PTO2Cat2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&TEXT".$i."=Value";
echo $url;
}
I have got like this issue with using file_get_contents that get url query for solr search engine. I just solved it by escaping the % percent sign in the url encoded string by adding an extra % before every % in the string as follows:
$str = "%s?q=WebSite:%s&sort=Date%%20desc&version=2.2&start=%s&rows=%s&indent=on&wt=json";
return sprintf($str, $this->url, $this->website, $this->start, $rows);
Notice the double % after the Date in the string.

Categories