Concatenate two different variable in PHP - php

I have the following code
<?php
$str="3dollars";
$a=20;
$a+=$str;
print($a);
?>
How can be the Output of the above program is 23 ?
Thanks In Advance !!

The + operator will coerce the string into being an integer, so it will (internally) do something like this:
$str = "3dollars";
$a = 20;
$a += $str;
// $str = (int)"3dollars";
// $str = 3;
$a = 23;
What you want to do is use the 'concatenation' operator (.):
<?php
$str = "3dollars";
$a = 20;
$a .= $str;
print($a); // 203dollars

It's taking "3dollars" as a number, getting $str = 3.
And when you echo, you add 20, to $str, so it prints 23 and $a = 23.
<?php $a += $str;
print($a);
it echo 23; //$a=$a+$b;?>

Use this
<?php
$a = 20;
$str = "3dollars";
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
$matches = $a + $matches[0][0];
echo '<br> Value Is :'.$matches;
?>
This will print 23 as Answer.
Output
Array ( [0] => Array ( [0] => 3 ) )
Value Is :23
phpfiddle Preview

$str= (int) "3dollars";
$a=20;
$a+=$str;
print($a);
Use (int) to convert string to integer.

Related

extracting part of a string in php

These are my possible strings.
$str = 'price-100-500';
$str = 'price-200-600';
I want two variables from the string.
From first string, I want
$val1 = 100;
$val2 = 500;
From second string, I want
$val1 = 200;
$val2 = 600;
Please, how can I get?
Use PHP explode function. See my codes as below:
$str = 'price-100-500';
$arr = explode('-',$str);
$val1 = $arr[1];
$val2 = $arr[2];
Just do in simple way:
$str = 'price-100-500';
list($val1, $val2) = explode('-', str_replace('price-', '', $str));
echo '<br>val1 : '.$val1;
echo '<br>val2 : '.$val2;
Output
val1 : 100
val2 : 500
Use
$str = 'price-100-500';
$exploded=explode("-",$str);
echo $exploded[1];//will echo 100
echo $exploded[2];//will echo 500
Similarly you can explode any string
Assuming that you are willing to have this from several similar $str, and if you have them as an array, you can use the following code block to get them all as array output --
foreach($strings as $string){
$arr = explode('-',$string);
$result[$string]['val1'] = $arr[1];
$result[$string]['val2'] = $arr[2];
}
print_r($result);
This will give you an output like
Array
(
[price-100-500] => Array
(
[val1] => 100
[val2] => 500
)
[price-200-600] => Array
(
[val1] => 200
[val2] => 600
)
)
For better and exact answer you should provide better explanation of your problem. Hope for the best.
Thanks...:)
For your first example:
<?php
$str = 'price-100-500';
$arrayStr=explode("-",$str);
$val1 = $arrayStr[1];
$val2 = $arrayStr[2];
?>
If u wan't a function you can use this:
<?php
$str = 'price-100-500';
$array = getMyVar($str);
echo $array[0];
echo $array[1];
function getMyVar($str){
$arrayStr=explode("-",$str);
$val1 = $arrayStr[1];
$val2 = $arrayStr[2];
return array($val1,$val2);
}
?>

Each value of a string to a separate array conversion

I have a html page contents, that I converted into a string separated by "#".
Example:
(2R)-2-hydroxy#250.181#C15H24NO2#2#1#46#1#11#1.1266#1#18#6
Is there any way to convert each value of these string to convert into a array?
I need an output like this:
$a = (2R)-2-hydroxy
$b = 250.181
$c = C15H24NO2
$d = 2
$e = 1
//etc...
This should work for you:
Just explode() your string and loop through the array. Then you can assign each value to a variable, where you can increment the character.
$str = "(2R)-2-hydroxy#250.181#C15H24NO2#2#1#46#1#11#1.1266#1#18#6";
$arr = explode("#", $str);
$start = "a";
foreach($arr as $v) {
$$start = $v;
$start++;
}
This should work
$array = explode("#", "(2R)-2-hydroxy#250.181#C15H24NO2#2#1#46#1#11#1.1266#1#18#6");
$array [0] = (2R)-2-hydroxy
$array [1] = 250.181
...

How to create array using customize value?

I have some values below:
11 12 13.
I need to make an array using this value.
array(11,12,13);
I tried this code below :
$selected is the variable that contain the value 11 12 13 //Special Instruction
foreach($selected as $key=>$val)
{
$sel.=$val;
$sel.=",";
}
$str = rtrim($sel,',');
// echo $str;
$shortlist = array_map('trim', explode(',',$str));
I need help to make an array like array(11,12,13).Any idea?
You can use explode here. split is deprecated in latest version.
$str = "11 12 13";
$array = explode(" ",$str);
try str_split see http://www.php.net/manual/en/function.str-split.php
$str = "111213";
$array = str_split($str, 2);
print_r($array);
output :
Array
(
[0] => 11
[1] => 12
[2] => 13
)
$values = explode(' ', "11 12 13");
// if there is no space, you can do it like this
$strLen = strlen($string);
$i = 0;
while($i < $strLen) {
$myArr[] = substr($string, $i, 2);
$i += 2;
}
print_r($myArr);
$selected = "11 12 13";
print_r (explode(" ",$selected));

Efficient check for array items in a string

I have an array like:
arr = array("*" , "$" , "and" , "or" , "!" ,"/");
and another string like :
string = "this * is very beautiful but $ is more important in life.";
I'm looking the most efficient way with the lowest cost to find the member of the array in this string. Also I need to have an array in result that can show which members exist in the string.
The easiest way is using a for loop but I believe there should be more efficient ways to do this in PHP.
$arr=array("*" , "$" , "#" , "!");
$r = '~[' . preg_quote(implode('', $arr)) . ']~';
$str = "this * is very beautiful but $ is more important in life.";
preg_match_all($r, $str, $matches);
echo 'The following chars were found: ' . implode(', ', $matches[0]);
If you are looking for the most efficient way, the result of the following code is:
preg:1.03257489204
array_intersect:2.62625193596
strpos:0.814728021622
It looks like looping the array and matching using strpos is the most efficient way.
$arr=array("*" , "$" , "#" , "!");
$string="this * is very beautiful but $ is more important in life.";
$time = microtime(true);
for ($i=0; $i<100000; $i++){
$r = '~[' . preg_quote(implode('', $arr)) . ']~';
$str = "this * is very beautiful but $ is more important in life.";
preg_match_all($r, $str, $matches);
}
echo "preg:". (microtime(true)-$time)."\n";
$time = microtime(true);
for ($i=0; $i<100000; $i++){
$str = str_split($string);
$out = array_intersect($arr, $str);
}
echo "array_intersect:". (microtime(true)-$time)."\n";
$time = microtime(true);
for ($i=0; $i<100000; $i++){
$res = array();
foreach($arr as $a){
if(strpos($string, $a) !== false){
$res[] = $a;
}
}
}
echo "strpos:". (microtime(true)-$time)."\n";
You can use array_insersect
$string = "this * is very beautiful but $ is more important in life.";
$arr=array("*" , "$" , "#" , "!");
$str = str_split($string);
$out = array_intersect($arr, $str);
print_r($out);
This code will produce the following output
Array ( [0] => * [1] => $ )

Add +1 to a string obtained from another site

I have a string I get from a website.
A portion of the string is "X2" I want to add +1 to 2.
The entire string I get is:
20120815_00_X2
What I want is to add the "X2" +1 until "20120815_00_X13"
You can do :
$string = '20120815_00_X2';
$concat = substr($string, 0, -1);
$num = (integer) substr($string, -1);
$incremented = $concat . ($num + 1);
echo $incremented;
For more informations about substr() see => documentation
You want to find the number at the end of your string and capture it, test for a maximum value of 12 and add one if that's the case, so your pattern would look something like:
/(\d+)$/ // get all digits at the end
and the whole expression:
$new = preg_replace('/(\d+)$/e', "($1 < 13) ? ($1 + 1) : $1", $original);
I have used the e modifier so that the replacement expression will be evaluated as php code.
See the working example at CodePad.
This solution works (no matter what the number after X is):
function myCustomAdd($string)
{
$original = $string;
$new = explode('_',$original);
$a = end($new);
$b = preg_replace("/[^0-9,.]/", "", $a);
$c = $b + 1;
$letters = preg_replace("/[^a-zA-Z,.]/", '', $a);
$d = $new[0].'_'.$new[1].'_'.$letters.$c;
return $d;
}
var_dump(myCustomAdd("20120815_00_X13"));
Output:
string(15) "20120815_00_X14"

Categories