I would like to use a php function for a string. Here's an example:
$txt = '123';
echo $fin = str_replace('2',"<?php echo 8; ?>",$txt);
I would like to obtain this:
183
But this is what I get:
13
Tr this
$txt = '123';
$fin = str_replace('2','8',$txt);
echo $fin;
You don't need <?php echo 8; ?>
juste do echo $fin = str_replace('2','8',$txt);
You can use a variable instead of a string if thats what you need:
$txt = '123';
$search = '2';
$replace = '8';
$end = str_replace($search,$replace,$txt);
echo $end;
This way you can use a function to change the search and change string if needed.
Related
I need to move a specific character in a string from a specific position to right by specific places. string, position, and places are inputs it can be any value.
(Maximum use less number of lines using PHP functions).
$string = 'Peacock';
$position = 2;
$places = 2;
move($string,$position,$places);
function move($string,$position,$places){
$string[($position-1)+$places] = $string[$position-1];
echo $string ;
}
Expected output is Paceock
with for loop will be a good solution.
NB: the $position must be start from 1.
<?php
$string = 'Peacock';
$position = 2;
$places = 2;
move($string,$position,$places);
function move($string,$position,$places){
$keep = $string[$position-1];
for($i=0; $i<=$places;$i++){
$string[($position-1)+$i] = $string[$position+$i];
}
$string[$position+$places-1] = $keep;
echo $string ;
}
If you do not mind you can modify the string as well.
$string = 'Peacock';
$position = 2;
$places = 2;
function move($string,$position,$places){
$char = $string[$position-1];
$string = substr_replace($string, '', $position-1, 1);
$string = substr_replace($string, $char, $position+$places-1, 0);
echo $string;
}
move($string,$position,$places);
I wonder how I can use the exact opposite of the eval function.
This is my code:
$test = 1;
$t = '$test';
echo opposite_eval($t);
I have to 1 output from above codes, how can i use method, function or class ?
Thanks for your estemeed help friends !
i cheated a little by removing the $ from the $t string (you can do that in the function its just a string:
$t = 'test';
function opposite_eval($t){
$test = 1;
return($$t);
}
echo opposite_eval($t); //=1
the phase you want to look in to is variable variables
I think you want a variables variable
In your case it would be:
$test = 1;
$t = 'test';
echo $$t;
// output: 1
Addon:
You could also do things like that:
$test['x'] = 1;
$t = 'test';
echo $$t['x'];
Whereas this will not work:
$test['x'] = 1;
$t = "test['x']";
echo $$t;
// Produces: NOTICE Undefined variable: test['x'] on line number 6
neither will:
$test = new stdClass();
$test->x = 1;
$t = "test->x";
echo $$t;
but this will work:
$test = new stdClass();
$test->x = 1;
$t = "{$test->x}";
echo $t;
and this will also work:
$test =[];
$test['x'] = 1;
$t = "{$test['x']}";
echo $t;
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
There is any possibility making something like that using PHP?
<?php
$number = 1;
$str_$number = "BlahBlah";
// and make this: $str_1 = "BlahBlah";
echo $str_1;
?>
<?php
$number = 1;
${'str_'.$number} = 'foobar';
echo $str_1;//foobar
<?php
$number = 1;
$value = 'str_' . $number;
$$value = 'blahblah';
echo $str_1;
?>
Try
<?php
$number = 1;
${'str_' . $number} = 'foobar';
?>
You can also try
$number = 1;
eval('$str_'.$number.' = "foobar";');
echo $str_1;
I'm trying to concatenate several variables following http://php.net/manual/en/language.variables.variable.php but I don't get why it's not working
<?PHP
$test1 = 'test1';
$test2 = 'test2';
$test0 = 'test0';
for($i=1;$i<=9;$i++){
$j = $i%3;
echo ${$test.$j};
}
?>
If at all possible it would be much easier to just set the variables up as an array in the first place.
But to do what you are trying do this
$test1 = 'test1';
$test2 = 'test2';
$test0 = 'test0';
for($i=1;$i<=9;$i++){
$j = $i%3;
echo ${"test".$j};
}
try it.
$test1 = 'test1';
$test2 = 'test2';
$test0 = 'test0';
for($i=1;$i<=9;$i++){
$j = $i%3;
echo $colonne.$j;
}
?>