Lets say i have url like this
$a= "http://zz.com/1/2/3/4/5/6/7";
say that url can have many step like 1 ,2 ,3 say something it have up to 3 or sometime up to 7
I want to get url like this from $a
$b="http://zz.com/";
$c="http://zz.com/1/";
$d="http://zz.com/1/2/";
$e="http://zz.com/1/2/3/";
...
...
$k= "http://zz.com/1/2/3/4/5/6/;
Is it possible to do such things in php?
Thank you very much .
I tried to use php url parse and explode but get empty value in beginning and end of array.
Very simple way is to use explode() function.
[EDIT] made it use letters as variables if that was necessary
$a= "http://zz.com/14/2/13/4/5/8/7";
//grab the protocol and addy
$x = explode('//',$a);
$y = explode('/',$x[1]);
$letters = array();
$letters[1] = 'a';
$letters[2] = 'b';
$letters[3] = 'c';
$letters[4] = 'd';
$letters[5] = 'e';
$letters[6] = 'f';
$letters[7] = 'g';
$letters[8] = 'h';
$letters[9] = 'i';
$letters[10] = 'j';
//loop through various steps
for($i = 1; $i<=count($y); $i++)
{
$$letters[$i] = $x[0].'//'.$y[0].'/';
for($k=0; $k<$i; $k++)
{
$$letters[$i] .= $y[$k].'/';
}
}
echo $a."\n";
echo $b."\n";
echo $c."\n";
echo $d."\n";
echo $e."\n";
echo $f."\n";
echo $g."\n";
echo $h."\n";
that will output:
http://zz.com/zz.com/
http://zz.com/zz.com/14/
http://zz.com/zz.com/14/2/
http://zz.com/zz.com/14/2/13/
http://zz.com/zz.com/14/2/13/4/
http://zz.com/zz.com/14/2/13/4/5/
http://zz.com/zz.com/14/2/13/4/5/8/
http://zz.com/zz.com/14/2/13/4/5/8/7/
You can see the code working here: http://sandbox.onlinephpfunctions.com/code/4c0446acaf0fd298fc089b743da5a807529e3e0b
[EDIT: with letters here]http://sandbox.onlinephpfunctions.com/code/bb655992fa81f0005938d86697e91272dc57425a
you can try this code:-
<?php
$a= $_SERVER['REQUEST_URI'];//"http://zz.com/1/2/3";
$url = explode('/',str_replace('http://', '', $a));
$next_num = count($url);
echo $next_url = $a.'/'.$next_num;//wil print :- http://zz.com/1/2/3/4
?>
Yes you can and it is pretty easy and it is good to use build in function called parse_url() which extracts some parts of URL.
$url = "http://zz.com/1/2/3/4/5/6/7";
$pathvars = parse_url($url);
$urls = [];
$glue = '';
$counter = 'a';
foreach ($path as $part) {
$glue .= "$part/";
$urls[$counter++] = sprintf("%s://%s%s", $pathvars['scheme'], substr($pathvars['host'], 0, strlen($pathvars['host']) -1), substr($glue, 0, strlen($glue) - 1));
}
// extract to variables
extract($urls, EXTR_OVERWRITE);
echo $a . PHP_EOL;
echo $b . PHP_EOL;
echo $c . PHP_EOL;
echo $d . PHP_EOL; // etc
print_r($urls); // all array of urls
Related
How do i turn an string into variables and still get a good output.
$text = "showandreturn";
$disp = str_split($text, 2);
$firstnum = 3;
for($b = 0; $b<$firstnum; $b++){
$a = "$disp[$b]"; #showan
}
$b = "ENDED";
for($b = $firstnum; $b<sizeof($disp); $b++){
$c = "$disp[$b]"; #dreturn
echo = "$a$b$c";
}
my current output with this code is . andranetanurann ..
And I want beter result like showanENDEDdreturn
Thanks for your time and understanding..
Are you trying to put ENDED in the middle of the string?
In that case have a look at this:
$text = "showandreturn";
$b = "ENDED";
$len = strlen($text); // lenght of string
// Output half string + $b + rest of string
Echo substr($text, 0, floor($len/2)) . $b . substr($text, floor($len/2));
https://3v4l.org/Ea6tn
You are overwriting your variables every time it loops so only the result of the last loop is stored in the variable.
This code should return the required output.
$text = "showandreturn";
$disp = str_split($text, 2);
$num = 3;
$a = '';
$c = '';
for($b = 0; $b<$num; $b++){
$a .= $disp[$b];
}
$b = "ENDED";
for($bn = $num; $bn<sizeof($disp); $bn++){
$c .= $disp[$bn];
}
echo $a.$b.$c;
Alternative 1
What you need is variables variable
$foo = 'bar';
$foo = 'magic';
echo $foo; //Outputs magic
Alternative 2:
you can check http://php.net/manual/en/function.parse-str.php
parse_str($"My Value=Something");
echo $My_Value; // Something
Alternative 3:
echo eval('return $'. $string . ';');
I have 2 files for example
file1.txt
and
file2.txt
file1.txt contains for example following content:
a:b
markus:lanz
peter:heinrichs
lach:schnell
and file2.txt contains for example following content (the 2nd explode of the file1.txt)
b:c
lanz:hallo
heinrichs:gruss
schnell:langsam
so i want to have following output in php:
a:c
markus:hallo
peter:gruss
lach:langsam
how is this possible?
first explode the first and then search or how?
thanks
my current code is following:
<?php
$file1 = 'a:b
markus:lanz
peter:heinrichs
lach:schnell';
$file2 = '
lanz:hallo
heinrichs:gruss
b:c
test:notest
schnell:langsam';
$array = explode(":", $file1);
for($i=0; $i < count($array); $i++) {
$array = explode(":", $file1);
$pattan = $array[$i];
$pattern = '=\n'. $pattan .':(.*)\n=sUm';
$result = preg_match($pattern, $file2, $subpattern);
echo "<br>";
echo $array[$i];
$first = $array[$i];
echo "<br>";
}
$pattern = '=\n'. $first .':(.*)\n=sUm';
$result = preg_match($pattern, $file2, $subpattern);
var_dump($subpattern);
?>
what am i making wrong?
What do you think about this :
$file1="a:b
markus:lanz
peter:heinrichs
lach:schnell";
$file2="b:c
lanz:hallo
heinrichs:gruss
schnell:langsam";
$a1 = explode("\n",$file1);
$a2 = explode("\n",$file2);
if(count($a1) != count($a2)) die("files don't match!");
$final=array();
foreach($a1 as $k=>$line){
$l1 = explode(":",$line);
$l2 = explode(":",$a2[$k]);
$final[]=$l1[0].':'.$l2[1];
}
print_r($final);
I would use the following approach:
// load your two files into arrays using file() and explode each line on ':'
foreach ([1,2] as $n) {
$files[$n] = array_map(function($x){ return explode(':', trim($x));}, file("file$n.txt"));
}
// with file1, fill the output array with 'a' values using 'b' as the key to output
foreach ($files[1] as $ab) {
$output[$ab[1]]['a'] = $ab[0];
}
// with file2, fill the output array with 'c' values, also using 'b' as the key
foreach ($files[2] as $bc) {
$output[$bc[0]]['c'] = $bc[1];
}
// remove any elements that do not have count 2 (i.e. both a and c values)
$output = array_filter($output, function ($x) { return count($x) == 2; });
I want to get a proper solution for my coding.
Step 1. I first break it into array
Step 2. then split each explode value
Step 3. then sort it by a for loop
<?php
$st ="It is a test";
$exp = explode(' ',strtolower($st));
print_r($exp);
$c =0;
$p =0;
for($i=0;$i<count($exp);$i++){
$t = str_split($exp[$i]);
$ch = $t[0];
if($c==0){
$arr[$p]=$exp[$i];
$temp = $ch;
}else{
if(ord($ch)<ord($temp)){
$tp = $arr[$p-1];
$arr[$p-1] = $exp[$i];
$arr[$p] = $tp;
}else{
$arr[$p] = $exp[$i];
}
}
$temp = $ch;
$p++;
$c++;
}
print_r($arr);
?>
Any help would be appreciated.
Not sure what you want, but I think this is what you want.
<?php
$st ="It is a test";
$exp = explode(' ',strtolower($st));
sort($exp);
for($x = 0; $x <count($exp); $x++) {
echo $exp[$x];
echo "<br>";
}
?>
I'm receiving a string
a=100,b=20,c=20;
for instance I know exploding this into an array using , as delimiter,
and it becomes
a=100
b=20
c=20
as strings,
how can this be further evaluated as variables inside php so I could do
echo $a //100
echo $b //20
echo $c //20
try the function "parse_str",like
$str = str_replace(',','&',"a=100,b=10,c=20");
parse_str($str);
echo $a;
echo $b;
echo $c;
$string = 'a=1,b=20,c=43';
foreach ( explode(',', $string) as $expression ) {
list($variable,$value) = explode('=', $expression);
$$variable = $value;
}
echo 'a = ', $a, ' and b = ', $b, ' and c = ', $c, PHP_EOL;
Cheers
Depends how "dangerous" you want everything to be - you can always use eval but then there are many issues to take into consideration.
Example:
eval("\$str = \"test\";");
echo $str;
Try This
$input = 'a=100,b=20,c=20';
$items = explode(',',$input);
foreach($items as $i){
$pair = explode('=',$i);
$$pair[0] = $pair[1];
}
$input = 'a=100,b=20,c=20';
$statements = explode (',', $input);
foreach ($statements as $s)
eval ('$' . $s); // THIS IS EVIL!!!! TOO MANY SECURITY PROBLEMS
echo $a; // will output 100
echo $b; // will output 20
echo $c; // will output 20
You can use variable names for variables. Take for example:
$var = 'var2';
$$var = 5;
echo $var2;
this echoes 5 to the command line.
$var evaluates to being var2, and $var2 is set to 5.
Taking your code we can create this:
$string = 'a=100,b=20,c=20';
$array = explode(',',$string);
foreach($array as $element) {
$temp = explode('=',$element);
$$temp[0] = $temp1;
}
echo $a.' '.$b.' '.$c;
This echoes the string '100 20 20'.
I am having the following problem. I have the numbers 1/2/3/4/5/6 and I want to separate them into two groups 1/3/5 and 2/4/6. The selection must take place based on the position. This part works ok. The problem comes when I want to group them again, when I use the implode function; it only sees the last number that was stored. I know it has something to do with me using this notation (I chose this way since the amount of numbers to classify varies every time):
$q++;
$row0 = $row0 + 2;
$row1 = $row1 + 2;
but I can't figure a way to fix it or another way to get the same result. Hopefully someone here can point me in the right direction. I left the complete code below.
<?
$string = "1/2/3/4/5/6";
$splitted = explode("/",$string);
$cnt = count($splitted);
$q=0;
$row0=0;
$row1=1;
while($cnt > 2*$q)
{
$p_row = implode(array($splitted[$row0]));
echo "$p_row <br>";
$i_row = implode(array($splitted[$row1]));
echo "$i_row <br>";
$q++;
$row0 = $row0 + 2;
$row1 = $row1 + 2;
}
$out = "implode(',', $i_row)";
var_dump($out);
?>
I missread the problem it seems. Instead I give this optimization.
$string = "1/2/3/4/5/6";
$splitted = explode("/", $string);
$group = array();
for ($index = 0, $t = count($splitted); $index < $t; ++$index) {
$group[$index & 1][] = $splitted[$index];
}
$oddIndex = $group[0]; //start with index 1
$evenIndex = $group[1]; //start with index 2
echo "odd index: "
. implode('/', $oddIndex)
. "\neven index: "
. implode('/', $evenIndex)
. "\n";
You can split the array into groups using % on loop index. Put each group in separate array. Here is example:
<?php
$string = "1/2/3/4/5/6";
$splitted = explode("/",$string);
$group_odd = array(); ## all odd elements of $splitted come here
$group_even = array(); ## all even elements of $splitted come here
for ($index = 0; $index < count($splitted); ++$index) {
## if number is divided by 2 with rest then it's odd
## but we've started calculation from 0, so we need to add 1
if (($index+1) % 2) {
$group_odd[] = $splitted[$index];
}
else {
$group_even[] = $splitted[$index];
}
}
echo implode('/', $group_odd), "<br />"; ## outputs "1/3/5<br />"
echo implode('/', $group_even), "<br />"; ## outputs "2/4/6<br />"
print_r($group_odd);
print_r($group_even);
?>
Based on their position? So, split based on the evenness/oddness of their index in the array?
Something like this?
<?php
$string = "1/2/3/4/5/6";
list( $evenKeys, $oddKeys ) = array_split_custom( explode( '/', $string ) );
echo '<pre>';
print_r( $evenKeys );
print_r( $oddKeys );
function array_split_custom( array $input )
{
$evens = array();
$odds = array();
foreach ( $input as $index => $value )
{
if ( $index & 1 )
{
$odds[] = $value;
} else {
$evens[] = $value;
}
}
return array( $evens, $odds );
}