I'm trying to get a part of the URL on my website
In this situation:
http://mywebsite/filexx/yyyyy/abaete/374
$url2 = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(preg_match("/\/(\d+)$/",$url2,$matches))
{
$meuid = $matches[1];
}
its works, but lets put 2 different situations:
http://mywebsite/filexx/yyyyy/abaete/374/?p=1
// i'm try to get the 374 (ID)
http://mywebsite/filexx/yyyyy/374/?p=1
// here the same
so I want to get last part if integer ( 374 )
or the part before the query 374/?p=1. So I want the 374.
Thanks.
I'll just make my comment an answer:
<?php
$string = 'http://mywebsite/filexx/yyyyy/abaete/374/?g=123';
$matches = array();
preg_match_all('/.*?\/(\d+)\/?/s', $string, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
?>
It will also ignore the /?getval1=1&getval2=2&etc=etc
Output
Array
(
[0] => Array
(
[0] => http://mywebsite/filexx/yyyyy/abaete/374/
)
[1] => Array
(
[0] => 374
)
)
$url = 'http://mywebsite/filexx/yyyyy/abaete/374/?p=1';
$explodedUrl = explode('/',$url);
$countArray = count($explodedUrl);
if(strpos($explodedUrl[$countArray-1],'?') === FALSE){
$yourId = $explodedUrl[$countArray-1];
} else {
$yourId = $explodedUrl[$countArray-2];
}
$yourId contains your Id
http://mywebsite/filexx/yyyyy/abaete/374
$url2 = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(preg_match('~([0-9]+)\/\?p=~', $url2, $matches))
{
$meuid = $matches[1];
}
This gets the numbers before /?p=.
Related
I need to compare URLs and remove duplicates from array but I want compare only host from url. I need skip http and https and www and others like last slash when I compare.
So when I have array:
$urls = array(
'http://www.google.com/test',
'https://www.google.com/test',
'https://www.google.com/example',
'https://www.facebook.com/example',
'http://www.facebook.com/example');
Result will be only
http://www.google.com/test
http://www.google.com/example
http://www.facebook.com/example
I tried to compare like :
$urls = array_udiff($urls, $urls, function ($a, $b) {
return strcmp(preg_replace('|^https?://(www\\.)?|', '', rtrim($a,'/')), preg_replace('|^https?://(www\\.)?|', '', rtrim($b,'/')));
});
But it return me empty array.
<?php
$urls = array(
'http://www.google.com/test',
'https://www.google.com/test',
'https://www.google.com/example',
'https://www.facebook.com/example',
'http://www.facebook.com/example');
$MyArray = [];
for($i=0;$i<count($urls);$i++) {
preg_match_all('/www.(.*)/', $urls[$i], $matches);
if (!in_array($matches[1], $MyArray))
$MyArray[] = $matches[1];
}
echo "<pre>";
print_r($MyArray);
echo "</pre>";
And the output is
Array
(
[0] => Array
(
[0] => google.com/test
)
[1] => Array
(
[0] => google.com/example
)
[2] => Array
(
[0] => facebook.com/example
)
)
trimmed and keeping only the host name
Try this approach :
<?php
function parseURLs(array $urls){
$rs = [];
foreach($urls as $url){
$segments = parse_url($url);
if(!in_array($segments['host'], $rs))
$rs[] = $segments['host'];
}
return $rs;
}
Then :
<?php
$urls = array(
'http://www.google.com',
'https://www.google.com',
'https://www.google.com/',
'https://www.facebook.com',
'http://www.facebook.com'
);
$uniqueURLs = parseURLs($urls);
print_r($uniqueURLs);
/* result :
Array
(
[0] => www.google.com
[1] => www.facebook.com
)
*/
You need to Loop through the URL's, Parse URL with PHP's url_parse() function and use array_unique to remove duplicates from array, so we are checking both the host and path ..
I have written a class for you:
<?php
/** Get Unique Values from array Values **/
Class Parser {
//Url Parser Function
public function arrayValuesUrlParser($urls) {
//Create Container
$parsed = [];
//Loop Through the Urls
foreach($urls as $url) {
$parse = parse_url($url);
$parsed[] = $parse["host"].$parse["path"];
//Delete Duplicates
$result = array_unique($parsed);
}
//Dump result
print_r($result);
}
}
?>
Using the Class
<?php
//Inlcude tghe Parser
include_once "Parser.php";
$urls = array(
'http://www.google.com/test',
'https://www.google.com/test',
'https://www.google.com/example',
'https://www.facebook.com/example',
'http://www.facebook.com/example');
//Instantiate
$parse = new Parser();
$parse->arrayValuesUrlParser($urls);
?>
You can do it in one file if you don't need to seperate files but you will have to remove include_once if you are using one php file. This class is also on PHP Classes, did it for fun !
Best of Luck !
I have thousands of urls which have ids i want to get only ids from url for example
This is my array
Array
(
[0] => http://www.videoweed.es/file/f62f2bc536bad
[1] => http://www.movshare.net/video/5966fcb2605b9
[2] => http://www.nowvideo.sx/video/524aaacbd6614
[3] => http://vodlocker.com/pbz4sr6elxmo
)
I want ids from above links
f62f2bc536bad
5966fcb2605b9
524aaacbd6614
pbz4sr6elxmo
I have use parse_url function but its return me path which include all things after slash(/) like /file/pbz4sr6elxmo
<?php
foreach($alllinks as $url){
$parse = parse_url($url);
echo $parse['path'];
}
?>
Output
/pbz4sr6elxmo
/video/5966fcb2605b9
/file/f62f2bc536bad
/video/524aaacbd6614
You can try with explode -
$alllinks = array
(
'http://www.videoweed.es/file/f62f2bc536bad',
'http://www.movshare.net/video/5966fcb2605b9',
'http://www.nowvideo.sx/video/524aaacbd6614',
'http://vodlocker.com/pbz4sr6elxmo'
);
foreach($alllinks as $url){
$temp = explode('/', $url);
echo $temp[count($temp) - 1].'<br/>';
}
Output
f62f2bc536bad
5966fcb2605b9
524aaacbd6614
pbz4sr6elxmo
This will only help if the the url structure is same, i.e. the last part is the id
If the URLs always ends with the id you can simply do
$url = 'http://www.videoweed.es/file/f62f2bc536bad';
$url_split = explode('/', $url);
$code = $url_split[count($url_split) - 1];
Try this:
$alllinks = array(
'http://www.videoweed.es/file/f62f2bc536bad',
'http://www.movshare.net/video/5966fcb2605b9',
'http://www.nowvideo.sx/video/524aaacbd6614',
'http://vodlocker.com/pbz4sr6elxmo'
);
foreach($alllinks as $url){
$parts = explode('/', $url);
echo end($parts).'<br/>';
}
I have to extract a string like this:
index.php?module=Reports&action=abc&rname=Instantpayment
Now my task is to extract report, action and rname value in PHP.
I have tried by using explode(), but I am not able to extract module.
How can I do it?
You could use parse_str() in this case:
$string = 'index.php?module=Reports&action=abc&rname=Instantpayment';
$string = substr($string, strpos($string, '?')+1); // get the string from after the question mark until end of string
parse_str($string, $data); // use this function, stress free
echo '<pre>';
print_r($data);
Should output:
Array
(
[module] => Reports
[action] => abc
[rname] => Instantpayment
)
$yourUrl="module=Reports&action=abc&rname=Instantpayment"
$exploded_array = array();
parse_str($yourUrl, $exploded_array);
$exploded_array['module'];
$exploded_array['action'];
$exploded_array['rname'];
Use $_GET to get query strings from the URL
echo $_GET['module']; //Reports
echo $_GET['action']; // abc
echo $_GET['rname']; // Instantpayment
For getting from the string try explode():
$str ='index.php?module=Reports&action=abc&rname=Instantpayment';
$e = explode('?', $str);
$e1 = explode('&', $e[1]);
foreach($e1 as $v) {
$ex = explode('=', $v);
$newarr[$ex[0]] = $ex[1];
}
print_r($newarr); // Use this array of values you want.
//Array ( [module] => Reports [action] => abc [rname] => Instantpayment )
echo $newarr['module'];
echo $newarr['action'];
echo $newarr['rname'];
You have to access the globale GET variable:
$_GET['module']
$_GET['action']
$_GET['rname']
Try this:
<?php
$temp = "index.php?module=Reports&action=abc&rname=Instantpayment";
$t1 = explode("=",$temp);
for ($i = 1; $i < sizeof($t1); $i++)
{
$temp = explode("&", $t1[$i]);
echo $temp[0] . "\n";
}
?>
As the title suggests, my goal is to add a rewrite statement in .htaccess to replace a dynamic substring in the middle of a URL. Additionally, I need all occurrences of "|" to be changed to "+".
For instance, I'd like to take this URL
www.domain.com/?t=p&c=some+string&skus=string1|string2|string3
And redirect it to:
www.domain.com/?q=string1+string2+string3
Basically, the substring to replace starts at "t=" and ends at "skus".
Is this possible? Is this easier to do with PHP?
You can use the parse_url function
this would do the trick
$url = "www.domain.com/?t=p&c=some+string&skus=string1|string2|string3";
echo "<pre>";
print_r($decoded = parse_url($url));
print_r($parsed = queryToArray($decoded['query']));
$parms = explode("|", $parsed['skus']);
print_r($parms);
echo "</pre>";
function queryToArray($qry)
{
$result = array();
//string must contain at least one = and cannot be in first position
if(strpos($qry,'=')) {
if(strpos($qry,'?')!==false) {
$q = parse_url($qry);
$qry = $q['query'];
}
}else {
return false;
}
foreach (explode('&', $qry) as $couple) {
list ($key, $val) = explode('=', $couple);
$result[$key] = $val;
}
return empty($result) ? false : $result;
}
the result should look like this
Array
(
[path] => www.domain.com/
[query] => t=p&c=some+string&skus=string1|string2|string3
)
Array
(
[t] => p
[c] => some+string
[skus] => string1|string2|string3
)
Array
(
[0] => string1
[1] => string2
[2] => string3
)
Use PHP built-in function when possible. So in this case use parse_url and parse_str.
$parts = parse_url($url);
parse_str($parts['query'], $query);
$params = explode('|', $query['skus']);
I have following records in text file, need to extract that record form text file and treat them as seperate array variables
r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8) seperated by pipe(|)
I need to represent that as array use seperately like below
$r1= Array
(
[0] => 1
[1] => 2
[2] => 3
)
$r2=Array
(
[0] => 4
[1] => 5
[2] => 6
)
I have no idea how to do it, is it possible in php?
Just a plain regular expression to break up the string, followed by an explode on each group:
if (preg_match_all('#(\w+)=\(([\d,]*)\)#', $s, $matches)) {
foreach ($matches[2] as $i => $groups) {
$group_name = $matches[1][$i];
$$group_name = array_map('intval', explode(',', $groups));
}
}
print_r($r1);
print_r($r3);
print_r($rn);
You can use Eval
//Assuming you can pull the content from text file using fread
$temp = "r1=(1,2,3)|r2=(4,5,6)";
$temp=str_replace("=","=array",$temp);
$split=explode("|",$temp);
echo "<pre>";
foreach($split as $k=>$v){
$v="$".$v.";";
//Evaluate a string as PHP code .i.e You will get r1,r2 as a variable now which is array
eval($v);
}
print_r($r1);
print_r($r2);
$data = "r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$arr = explode("|", $data);
$finArray = array();
foreach($arr as $key=>$value)
{
$single = explode('(', $value);
$finArray[] = explode(',', str_replace(')', '', $single[1]));
}
print_r($finArray);
can be done as:
$string="r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$string=str_repla("r1=","",$string);
$yourArray=explode('|', $string);
This code will help you:--
<?php
$file = "/tmp/file1.txt"; // this is your file path
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
print $line;
$a=explode('|',$line);
print_r($a); // I have explode based on | for you...
foreach($a as $key=>$value)
{
print_r($value);
}
fclose($file);
}
?>
""or""
$a="r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$a=explode('|',$a);
print_r($a);
<?php
$file = "file.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
$str = $line;
}
$str1 = explode("|",$str);
foreach($str1 as $temp) {
$str2 = explode("=",$temp);
$data[$str2[0]] = explode(",",trim($str2[1],"()"));
}
echo '<pre>';
print_r($data);
echo '</pre>';
?>
This will do your job.