Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Suppose I have 3 different urls. All are illustrated below.
1. http://www.example.com/foo/bar/index.php?id=1
2. http://www.example.com/bar/foo/index.php?id=1
3. http://www.example.com/foo/contact.php
How can I check whether the given url contains foo as of first param.
I mean how can I get only 1st and 3rd url as true and 2nd as false.
Method 1: Use parse_url():
$array = parse_url($_SERVER['REQUEST_URI']);
$path = explode('/', $array['path']);
if ($path[1]==='foo')
{
// Here ya go! :-)
}
Method 2: Use a Regular Expression:
if (preg_match('/^http:\/\/www\.example\.com\/foo\/.*$/', $_SERVER['REQUEST_URI'])
{
// Here ya go! :-)
}
https://regex101.com/r/jZ0pL1/1
To get your url parameters first we will need to parse your url
$parsedUrl = parse_url($_SERVER['REQUEST_URI']);
Now we will split it into parts
$params = explode('/', $parsedUrl['path']);
And finally, to get the first parameter
// Getting the second key => value pair
$firstParam = $params[1];
To view all parameters
print_r($params);
You can use parse_url() as below,
$arr = parse_url($url);
You will get $arr[2]= 'foo', check the requested url using this and that is what you want...
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to retrive a selected part from a url . I've used substr method and i've successfully get the character. But my issue is that ,this is my sample url localhost/xxxxxxx/sugar_daddy_member-1.xml i need to retrive the last number in the url. By using this below given code i can sucessfully get the number but if two digit number comes in the url i can retrive only one number.
$page = 'sugar_daddy_member-10';
$last_char = substr($page, 19, 1);
You can use the strrpos function to find the dash.
<?php
$page = 'sugar_daddy_member-10';
$idx = strrpos($page, '-');
$last_char = substr($page, $idx+1);
echo ($last_char);
?>
Output
10
Try this one using PHP explode() Function
<?php
$page = 'sugar_daddy_member-10';
$temp = explode("-",$page);
echo $temp[count($temp)-1];
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I can't seem to get this correct.
I have this PHP command that I need to do the same thing in Python:
preg_match("/^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$/", $domain, $matches);
I have three possible string formats:
domain = collabedge-123.dc-01.com
domain = cb123.dc-01.com
domain = ss123.dc-01.com
I need to pull out the 123 and 01 from the string no matter what the format of the string and assign to variables.
You can use this code:
import re
domain = "collabedge-123.dc-01.com"
# preg_match("/^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$/", $domain, $matches);
regex = r"^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$"
res = re.match(regex,domain)
print(res.group(0))
print(res.group(1))
print(res.group(2))
print(res.group(3))
Output:
collabedge-123.dc-01.com
collabedge-
123
01
As sure as I posted the question I kept trying and figured it out. For those that want to know the answer.
d = re.match(r"(collabedge-|cb|ss)([0-9]+)\.dc-([0-9]+)\.com", domain)
firstnum = d.group(2)
secnum = d.group(3)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I loaded a websites html onto a string and it has several g.load() code in the body - maybe around 10. I need this particular one which has an identifier of divId:"listing-provided-by-module".
g.load({ajaxURL:"/AjaxRender.htm?encparams=4~502222143586867513~LVuCHKHFeed3jCcefsa9MDj3xIs5wDqP7UwvtV3XDO0HnrynNRzT338AKMnzqNa4bTpgvQbff_Phk5wkav9LlWUqZfiIFKl3zXnXawc1_XDPR_9F83BlTaqhCqbfubm40s0ZciFJZV2dHzDDwlDVJJzitcXFgThESVdjnWUjJkj_MuZSVclGh7ddZ0neIHCH&rwebid=46328989&rhost=1",jsModule:"z-complaint-manager-async-block",phaseType:"scroll",divId:"listing-provided-by-module"});
What I need exactly is the ajaxURL and at the same time it checks that it is indeed with divId:"listing-provided-by-module" so it gets the correct url. Hope you can help with the regex and PHP for this. Thanks!
I've tried:
/g\.load\(\{ajaxURL:"(.*)".*divId:"listing-provided-by-module"/
But the match is too wide.
Here is the whole HTML body I want to match against: http://pastebin.com/seJd2jjc
Try this (visualized here):
'/g\.load\(\{ajaxURL:"([^"]*)",[^})]*,divId:"listing-provided-by-module"/s'
Your URL will be contained in the first capture group.
So, the PHP code would be:
<?php
$matches = [];
if (preg_match('/g\.load\(\{ajaxURL:"([^"]*)",[^})]*,divId:"listing-provided-by-module"/s', $string, $matches))
{
$url = $matches[1];
}
else
{
// No match found.
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to get a substring (see examples, bold part) from the string. All strings begin with "input" followed by 2 underscores with some (1 to 7) random chars between. Thank you!
Examples:
input_7ax8_SOME_INFO
input_3f0max2_SOME_OTHER_INFO
input_k_ANOTHERINFO-any-chars-possible:0123456789
Using the detection of "non underscore" + "underscore" times 2 and fetching everything that comes after that you can get the result you ask.
The ?: is meant for not returning the result of the parts with underscores because the () are needed to combine it together.
$input = 'input_k_ANOTHERINFO-any-chars-possible:0123456789';
preg_match( '~^(?:[^_]+_){2}(.*)$~', $input, $match );
var_export($match);
You just need explode and its third param :
<?php
$input = 'input_7ax8_SOME_INFO';
$input = explode("_",$input,2); // Split 2 times
$input[2] = '<b>'.$input[2].'</b>'; // Make the rest of the string bold
$input = implode("_",$input); // re joining
echo $input;
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How i cant get a specific part of the current url? for example, my current url is:
http://something.com/index.php?path=/something1/something2/something3/
Well, i need to print something2 with php.
Thanks!
You use the explode function in PHP to separate the URL by the first parameter (in this case a forward slash). To achieve your goal you could use;
$url = "http://something.com/index.php?path=/something1/something2/something3/";
$parts = explode('/', $url);
$value = $parts[count($parts) - 2];
All these other example seem to focus on your exact example. My guess is that you need a more flexible way of doing this, as the explode-only approach is very fragile if your URL changes and you still need to get data out of path parameter in query string.
I will point out the parse_url() and parse_str() functions to you.
// your URL string
$url = 'http://something.com/index.php?path=/something1/something2/something3/';
// get the query string (which holds your data)
$query_string = parse_url($url, PHP_URL_QUERY);
// load the parameters in the query string into an array
$param_array = array();
parse_str($query_string, $param_array);
// now you can look in the array to deal with whatever parameter you find useful. In this case 'path'
$path = $param_array['path'];
// now $path holds something like '/something1/something2/something3/'
// you can use explode or whatever else you like to get at this value.
$path_parts = explode('/', trim($path, '/'));
// see the value you are interested in
var_dump($path_parts);
You could do something like this:
$url = explode('/', 'http://something.com/index.php?path=/something1/something2/something3/');
echo $url[5];