Using strpos to change variable content - php

I'm trying to remove the contents of a variable if it already exists in a string:
$baseurl = "http://mywebsite.ex/";
$b = $baseurl."http://";
$a = $b."http://mywebsite.ex";
if (strpos($a,$b) !== false)
{
echo 'true <br>';
$baseurl = "";
echo $a;
}
But when I test the script I get:
true
http://mywebsite.ex/http://http://mywebsite.ex
I expected the result:
true
http: //mywebsite.ex
Where am I wrong?

With strpos() you only detect if $b occurs somewhere in $a, but it doesn't remove it. To remove it you can assign the return value of strpos() to a variable and then cut out $b from $a with substr_replace(), e.g.
if (($position = strpos($a,$b)) !== false)
{
echo 'true <br>';
$baseurl = "";
$a = substr_replace($a, "", $position, strlen($b));
echo $a;
}
With this you will remove the first occurence of $b in $a. And if you would want to remove all occurences, just use str_replace(), e.g.
if (strpos($a,$b) !== false)
{
echo 'true <br>';
$baseurl = "";
$a = str_replace($b, "", $a);
echo $a;
}

I don't know what you are trying to do, but I think you have some logical problems.
Update. OK now I know what you wanted ;), I think #Rizier123: you nailed it.
What you do in you code is:
strpos(): you are asking in this if ( strpos( $a, $b ) !== false ) condition if $b ( http://mywebsite.ex/http:// ) is in $a ( http://mywebsite.ex/http://http://mywebsite.ex )
// this is always true because you concated the string like $a = $b . "http....., so $b is always in $a
Try this and take a look at the output:
$baseurl = "http://mywebsite.ex/";
$b = $baseurl . "http://"; // b looks like http://mywebsite.ex/http://
var_dump( $b );
$a = $b . "http://mywebsite.ex"; // a looks like http://mywebsite.ex/http://http://mywebsite.ex
var_dump( $a);
// strpos: you asking in this condition if $b ( http://mywebsite.ex/http:// ) is in $a ( http://mywebsite.ex/http://http://mywebsite.ex )
// this is always true because you concated the string like $a = $b . "http....., so $b is always in $a
if ( strpos( $a, $b ) !== false ) {
echo 'true <br>';
$baseurl = "";
echo $a;
}

Related

Replace variable by another variable in PHP

Here is my situation, let's say i have two variables:
$a = "Peter";
$b = "tall";
Then, i can combine this by
$c = " $a is very $b ";
which give me the string : "Peter is very tall"
However, if i have another variable $e passed to my function, and the string is " $a is very $b "
then, i print out $e, it just show me " $a is very $b ";
What i expected is "Peter is very tall", because $e is equal to $c.
This is the whole logic flow:
$e = " $a is very $b ";
getMsg($e);
function getMsg($e){
$a = "Peter";
$b = "tall";
$c = " $a is very $b ";
echo $c //Peter is very tall
echo $e //$a is very $b
}
How can i achieve this function?
You could use sprintf sprintf documentation. The %s symbols are string tokens that get substituted in the format string (the 1st param to sprintf).
$e = "%s is very %s";
getMsg($e);
function getMsg($e){
$a = "Peter";
$b = "tall";
echo sprintf($e, $a, $b);
}
Or a less coupled version of the getMsg() function:
function getMsg($msg,$name,$is){
echo sprintf($msg, $name, $is);
}
getMsg("%s is very %s", "Peter", "tall");
Please try this code. I hope this is what you want.
<?php
// Your code here!
$a = "Peter";
$b = "tall";
$e = $a."is very".$b;
getMsg($e,$a,$b);
function getMsg($e,$a,$b)
{
$c = $a."is very".$b;
echo $c;
echo $e;
}
?>
Try this. It should work fine. You need to Define $a and $b outside the function.
function getString($a,$b,$e){
$c = " $a is very $b ";
echo $c;
echo '<br/>'.$e;
}
$a = "Peter";
$b = "tall";
$e = " $a is very $b ";
$response = getString($a,$b,$e);
OUTPUT :
Peter is very tall
Peter is very tall

PHP Convert string (1234) to integer

So in php $a = 1234; and $a = (1234); are both valid integers, 1234.
I have a situation with some third party code where I have $a = "(1234)"; (ie, a string)
The normal converting string to int don't work (because of the brackets)
<?php
$b = (int) $a; // 0
$b = intval($a); // 0
I could do something like
preg_match('/^\(([\d]+)\)$/', $a, $m);
$b = $m[1];
Just wondering if there there some clever way of converting $a back into an integer that I have missed?
The one more option can be
$str = "(1234)";
$int = (int) trim($str, '()');
This will make sure that if it has () that it makes it a negative number.
$a = '1234';
if (0 !== preg_match('/^\((\d+\))$/', $a, $matches)) {
$b = (int)-$matches[1];
} else {
$b = (int)$a;
}

PHP use result of as variable

Is it possible to use the result of an if with an OR statement as a variable for a function?
As example:
$a = true;
$b = false;
if ($a || $b) {
$this->functionCall($a)
}
Other example:
$a = false;
$b = true;
if ($a || $b) {
$this->functionCall($b)
}
Third and final exmaple:
$a = true;
$b = true;
if ($a || $b) {
$this->functionCall($a, $b)
}
So I need to detect what variable is true and pass it as a paramater. Is this even possible?
Any helps is appreciated!
Many thanks in advance
I'd do the logic bit inside a two-parameter function if I were you, as such :
function myFunc($a = false, $b = false) {
if ($a == true)
echo 'a';
if ($b == true)
echo 'b';
}
myFunc(); // echoes nothing
$a = true;
$b = false;
myFunc($a, $b); // echoes 'a'
$a = false;
$b = true;
myFunc($a, $b); // echoes 'b'
$a = true;
$b = true;
myFunc($a, $b); // echoes 'ab'
PHP 5.6+ version, filter out the falsely values (you can pass a callback to array_filter for different checks) and use those with the splat operator.
$params = array_filter([$a, $b]);
$this->callFunction(...$params);
No need for any IF checks and confusing in IF assignments.
Explore Variadic functions and Argument unpacking.

Not sure what the value of a is

where:
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
I'm not sure how to work out a.
So I understand that this is a shorthand operator, and usually it's a case of:
$value ? true : false
meaning
if $a = true { true } else { false };
so:
if $a{
if $a{
true;}
else{
0;};
else{
if $0{
$a;}
else{
true;}
};
does this make the value of $a true?
The value of $a would be true
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
The shorthand can be interpreted like this:
if($a) {
if($a) {
$a = $b;
} else {
$a = $c;
}
} else {
if($c) {
$a = $a;
} else {
$a = $b;
}
}
Because $a is false for not existing in the first place, it immediately jumps to the else statement in that. So the only part that matters to you is:
if($c) {
$a = $a;
} else {
$a = $b;
}
0 is the same as false, so $c will come back as false, therefore $a is equal to $b, which is true.
Edit:
There is some discussion on the notice that is thrown, but this fails to account for the fact that notices are not truly errors and because of this there is no interruption to the code. The result is not Notice: Undefined variable: a, the "result" (think these people mean output) would be blank if it weren't for us determining the value of $a at the end with var_dump. The question was as to what the value of $a becomes, not what appears on your screen.
Something displaying on your screen in re to a variable not being set has nothing to do with the value of what $a is.
If you execute the following code, the notice is not the only thing realized:
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
var_dump($a);
So the output is:
E_NOTICE : type 8 -- Undefined variable: a -- at line 5
bool(true)
The fact that a notice was thrown does not prevent $a from becoming true.
Also notices are easily suppressed...
error_reporting(0);
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
var_dump($a);
would result in $a still becoming true, and without seeing the notice.
bool(true)
If you run the code as is, you would get: Notice: Undefined variable: a in myfile.php on line 4
Therefore, I would postulate $a is set somewhere earlier. Yet, whatever value $a has prior, if $a is can be evaluated to true or false, $a would still be true after running your code for the following reason:
If $a were true, then the first part would yield $a = $b and we know $b = true.
if(TRUE) {
if(TRUE) {
$a = $b; //AND $b == TRUE
} else {
$a = $c;
}
} else {
...
}
If $a were false, then the second part would yield $a = $b again
if(FALSE) {
...
} else {
if(0) { // 0 will equate to FALSE
...
} else {
// 0 is the same as FALSE so we end up again with $a = $b
$a = $b; //AND $b == TRUE
}
}
In fact, if you run this code, it will show you the value of $a is true both times:
<?php
$a = false;
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
echo $a;
$a = true;
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
echo $a;

Insert variable into string at random position

See this code:
<?php
$a = rand(1, 10000000000);
$b = "abcdefghi";
?>
How can I insert $b into a random position of $a?
Assuming "casual" means random:
<?php
$a = rand(1, 10000000000);
$b = "abcdefghi";
//get a random position in a
$randPos = rand(0,strlen($a));
//insert $b in $a
$c = substr($a, 0, $randPos).$b.substr($a, $randPos);
var_dump($c);
?>
above code working: http://codepad.org/VCNBAYt1
Edit: had the vars backwards. I read "insert a into b,
I guess you could by treating $a as a string and concatenating it with $b:
$a = rand(1, 1000000);
$b= "abcd";
$pos = rand(0, strlen($a));
$a = substr($a, 0, $pos).$b.substr($a, $pos, strlen($a)-$pos);
and the results:
a=525019
pos=4
a=5250abcd19
a=128715
pos=5
a=12871abcd5
You should put {$b} on top of {$a} so that you can insert it to {$b}..
eg:
<?php
$b = "abcdefghi";
$a = rand(1, 10000000000);
$a .= $b;
echo $a;
?>
Sth like this :
<?php
$position = GetRandomPosition(); // you will have to implement this function
if($position >= strlen($a) - 1) {
$a .= $b;
} else {
$str = str_split($a, $position);
$a = $str[0] . $b . implode(array_diff($str, array($str[0])));
}
?>
Cast $a to string, then use strlen to get the length of $a. Use rand, with with the length of $a as the maximum, to get a random position within $a. Then use substr_replace to insert $b into $a at the position you've just randomized.

Categories