I have a string $string = 'contact['.'firstname'.']';
How can I get the contact and firstname from that string.
Sorry about my English, It's hard to describe the question
<?php
$string = 'contact['.'firstname'.']';
$a = explode('[',substr($string,0,-1));
echo $a[0];
echo '<br />';
echo $a[1];
?>
Use explode
$result = explode(chr(91),substr($string,0,-1));
then $result[0] and $result[1] will have what you want
Related
I need to append a text at the end position of the search term.
$baseStr = 'www.abc.com/cdf/?x=10';
$searchStr = 'www.abc.com/';
$insertStr = 'xxx/';
I need to insert $insertStr after 'www.abc.com/' in $baseStr. I get only the start position using strpos.
Expected result:
$resultStr = 'www.abc.com/xxx/cdf/?x=10';
Edit:
Is it possible to find the end position of the search string and solve this?
You can just replace $searchStr with $searchStr plus $insertStr
$baseStr = 'www.abc.com/cdf/?x=10';
$searchStr = 'www.abc.com/';
$insertStr = 'xxx/';
$resultStr = str_replace($searchStr, $searchStr.$insertStr, $baseStr);
echo $resultStr;
gives
www.abc.com/xxx/cdf/?x=10
Use preg_replace here:
$baseStr = 'www.abc.com/cdf/?x=10';
$searchStr = 'www.abc.com/';
$insertStr = 'xxx/';
$a = preg_replace('#'.$searchStr.'#', $searchStr.$insertStr, $baseStr);
echo '<pre>';
print_r($a);
//Output: www.abc.com/xxx/cdf/?x=10
Or you can use str_replace:
$baseStr = 'www.abc.com/cdf/?x=10';
$searchStr = 'www.abc.com/';
$insertStr = 'xxx/';
$a = str_replace($searchStr, $searchStr.$insertStr, $baseStr);
echo '<pre>';
print_r($a);
//Output: www.abc.com/xxx/cdf/?x=10
You can do something like this,
echo str_replace($searchStr, $searchStr.$insertStr,$baseStr);
Just replace your string it will search and replace for you.
Demo.
I have string:
$my_string = 'kqxs.mt={run:1,tinh:"39,31",ntime:153801582,delay:2000,kq:{39:{lv:"K42",8:"69",7:"985",6:["7772","4105","0258"],5:"8965",4:["03787","86098","45665"]}}};';
Please help me convert this string to array in PHP.
I want result:
array(){
[8]{
[0]=>69
},
[7]{
[0]=>985
},
[6]{
[0]=>7772
[1]=>4105
[2]=>0258
}
.............
}
Thank you!
You can just treat the string like it's an array:
<?php
$my_string = 'kqxs.mt={run:1,tinh:"39,31",ntime:153801582,delay:2000,kq:{39:{lv:"K42",8:"69",7:"985",6:["7772","4105","0258"],5:"8965",4:
"03787","86098","45665"]}}};';
//
for ($x = 0; $x < strlen($my_string); $x++){
echo "<br>".$my_string[$x];
}
?>
You need a separator to make a string to became array.
For example: $string = "Hello, beautiful, world";
to make the $string array you need to use explode.
For example: $string_array = explode(",", $string);
The first parameter in explode will be your separator and your second parameter is the string you want to become array.
I have this elements where I need to extract this {{search_tag}} and replace by a value
https://www.toto.com/search/10/{{search_tag}}.html#_his_
I tried this but I don't if it's the good way, does'nt work.
$words = explode('{{search_tag}} ',$website_url[$n]);
$exists_at = array_search($seach,$words);
if ($exists_at){
echo "Found at ".$exists_at." key in the \$word array";
}
You can use str_replace
$str = 'https://www.toto.com/search/10/{{search_tag}}.html#_his_';
$val = 'NEW_VALUE';
$new_str = str_replace("{{search_tag}}", $val, $str);
//outputs: https://www.toto.com/search/10/NEW_VALUE.html#_his_
I have a string tokenNo=12345&securityCode=111&name=Sam
How is it possible to extract the datas and store them into variables as below?
$tokenNo='12345';
$securityCode='111';
$name='Sam';
Please help
Use parse_str:
$string = 'tokenNo=12345&securityCode=111&name=Sam';
parse_str($string);
echo $tokenNo;
echo $securityCode;
echo $name;
Or you can store the entire variables in one array:
$string = 'tokenNo=12345&securityCode=111&name=Sam';
parse_str($string, $array);
print_r($array);
parse_str($string, $array);
print_r($array);
Use
parse_str()
$string = 'tokenNo=12345&securityCode=111&name=Sam';
parse_str($string);
echo $name."<br>";
echo $tokenNo."<br>";
echo $securityCode.;
I have a returning values that give me double digits, tripple digits etc. I want to put a "w" in between those numbers. Any kind of help I get on this is greatly appreciated.
$value = "444";
I want it to give me back this:
$value = "4w4w4w";
You could convert the string into an array and them implode it:
echo implode("w", str_split("444")) . "w";
Lets say $inp holds your number.
$arr = str_split($inp);
$result = implode('w',$arr);
str_split() and implode() will help.
$var = "444";
$array = str_split($var);
$final = implode("w", $array);
In this case you $final = "4w4w4";, so you might want to append an extra "w" to the end.
Here's a short way to do it with regex:
$str = '444';
echo preg_replace ( '/(.)/', '$1w', $str );
try this
$value="444";
$out='';
for($i=0;$i<strlen($value);$i++){
$out.=$value[$i]. "w";
}
echo $out;
output
4w4w4w