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;
}
Related
How check in PHP is my string $a at postion $i is string $b
$a = "Ha me duck who,garage?!"
$b = "duck"
$i = 7;
echo function($a,$b,$i); // will return true, but for other $i false
Since you want to check at multiple occurrences, this soln makes a substring at expected index (with length of needle) and checks if the strings match.
<? php
$a = "Ha me duck who,garage, duck?!";
$b = "duck";
$i = 23;
var_dump(chk($a, $b, $i));
function chk($a, $b, $i) {
return substr($a, $i, strlen($b)) === $b;
}
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;
}
Adding a string with a variable $a = 'ABC-01-222222'; with $b = 1; and it should give $a = 'ABC-01-222223'
You can use explode() to split the value of $a into three parts. Then add $b to the third item of the array, and then re-join the parts using implode():
$a = 'ABC-01-222222';
$b = 1;
$parts = explode('-', $a);
$parts[2] += $b;
$a = implode('-', $parts);
echo $a;
I have two valriables in
$a="1:2:3";
$b="1:3:4:5";
Is there any simple method to add 4 and 5 in variable $a. Means i want the value of variable to be
$a="1:2:3:4:5"
A one line solution:
$result = implode(':', array_unique(array_merge(explode(':', $a), explode(':', $b))));
An even shorter one would be:
$result = implode(':', array_unique(array_merge(explode(':', "$a:$b"))));
$a2 = explode(":" , $a);
$b2 = explode(":" , $b);
foreach($b2 as $val)
{
if(in_array($val , $a2))
//do what you want
}
try this
$a="1:2:3";
$b="1:3:4:5";
$a = explode(':', $a);
$b = explode(':', $b);
$c = array_unique(array_merge($a,$b));
$a = implode(':', $c);
echo $a;
I notice that $a is ordered, so you can apply sort to the new array
$sort = SORT_NUMERIC;
$a = implode(':',array_uniqe(array_merge(explode(':',$a),explode(':',$b)),$sort));
See array_unique to other possible sorts.
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.