I have got strings like:
name="n e" content="12" icon="favicon.ico"
What is the best and quickest way to parse it as such array:
Array
(
[name] => "n e"
[content] => "12"
[icon] => "favicon.ico"
)
This should do it, using preg_match_all() to get all the groups and array_combine() to form the final array:
if (preg_match_all('/(\w+)="([^"]*)"/', $str, $matches)) {
return array_combine($matches[1], $matches[2]);
} else {
return array();
}
Edit
This alternative breaks when there are spaces in between the double quotes; otherwise it works as well:
parse_str(str_replace(array(' ', '"'), array('&', ''), $s), $a);
return $a;
Related
I have different strings in which I want to find specific number. If the number is within the string it should print that number.
Strings are like these:
string(9) "path_12.0"
string(9) "path_12.1"
string(9) "path_13.0"
string(9) "path_13.1"
string(9) "path_13.2"
Numbers are like:
int(12)
int(12)
int(13)
int(13)
int(13)
What I tried is:
if (strpos(','.$mainString.',' , ','.$QuestionId.',') != FALSE) {
echo $QuestionId;
} // this doesn't print anything in the body
I also tried the below trick but it also doesn't print anything
if(in_array($QuestionId, explode(',', $mainString))) {
echo $QuestionId;
}
I want to check something like this:
if($questionId is in $mainString) {
echo $questionId;
}
Note: I searched similar questions on StackOverflow but I didn't found a solution which solved my issue, therefore I'm posting this question.
Another option could be to create an array with your strings and use preg_grep with a pattern that checks if the first part of the decimal is equal of one of the numbers.
Example of a pattern where the digits from the array are used as an alternation:
_\K(?:12|13)(?=\.\d+)
_\K Match underscore and forget what was matched
(?: Non capturing group
12|13 Match either 12 or 13
) Close non capturing group
(?=\.\d+) Positive lookahead, assert what is directly on the right is a dot and 1+ digits
For example:
$numbers = [12, 13];
$strings = [
"path_12.0",
"path_12.1",
"path_13.0",
"path_13.1",
"path_13.2",
"path_14.1"
];
$pattern = "/_\K(?:" . implode('|', $numbers) . ")(?=\.\d+)/";
$resullt = preg_grep($pattern, $strings);
print_r($resullt);
Result
Array
(
[0] => path_12.0
[1] => path_12.1
[2] => path_13.0
[3] => path_13.1
[4] => path_13.2
)
Php demo
Or if you want to print the numbers only, you might use array_reduce and collect the matches:
$result = array_reduce($strings, function($carry, $item) use ($pattern){
if (preg_match($pattern, $item, $matches)){
$carry[] = $matches[0];
}
return $carry;
});
print_r($result);
Result
Array
(
[0] => 12
[1] => 12
[2] => 13
[3] => 13
[4] => 13
)
Php demo
You can use below snippet,
$paths = ["path_12.0", "path_12.1", "path_13.0", "path_13.1", "path_13.2", ];
$nos = [12, 12, 13, 13, 13, ];
function strpos_arr($needle,$haystack)
{
if (!is_array($haystack)) {
$haystack = [$haystack];
}
foreach ($haystack as $what) {
if (($pos = strpos($what,(string)$needle)) !== false) {
return $pos;
}
}
return false;
}
foreach ($nos as $key => $value) {
// checking if question id in in path array with str pos
if(strpos_arr($value,$paths) !== false){
echo $value."\n";
}
}
Demo.
$array_strings = ["path_12.0", "path_12.1", "path_13.0", "path_13.1", "path_13.2"];
$array_numbers = [12, 22, 13, 11, 17];
$results = [];
foreach ($array_strings as $string){
preg_match_all('!\d+\.*\d*!', $string, $matches);
foreach ($array_numbers as $number){
if (in_array($number, $matches[0])){
array_push($results, $number);
}
}
}
print_r($results);
results: Array ( [0] => 12 [1] => 13 )
Note 1: array answers can have duplicate values.
I have this cases with strings in PHP:
*nJohn*sSmith*fGeorge#*nHenry*sFord
and wish to create an array with
[name],[surname],[fathers] as indexes so it will produce
name_array[1] = (
[name] => 'John',
[surname] => 'Smith',
[fathers] => 'George'
)
name_array[2]=(
[name] => 'Henry',
[surname] => 'Ford'
)
and so on.
How to do it using preg_split in PHP??
Thanks!
I'd use preg_match_all to get the names. If your string is consistent I think you could do:
$string = '*nJohn*sSmith*fGeorge#*nHenry*sFord';
preg_match_all('/\*n(?<givenname>.*?)\*s(?<surname>.*?)(?:\*f(?<middlename>.*?))?(?:#|$)/', $string, $matches);
print_r($matches);
Regex demo: https://regex101.com/r/1hKzvM/1/
PHP demo: https://eval.in/784879
Solution without using regex:
$string = '*nJohn*sSmith*fGeorge#*nHenry*sFord';
$result = array();
$persons = explode('#', $string);
foreach ($persons as $person) {
$identials = explode('*', $person);
unset($r);
foreach ($identials as $idential) {
if(!$idential){
continue; //empty string
}
switch ($idential[0]) { //first character
case 'n':
$key = 'name';
break;
case 's':
$key = 'surename';
break;
case 'f':
$key = 'fathers';
break;
}
$r[$key] = substr($idential, 1);
}
$result[] = $r;
}
This function will produce the result that you want ! but consider it's not the only way and not the 100% correct way ! i used preg_split as u asked
function splitMyString($str){
$array_names = [];
$mainString = explode('#', $str);
$arr1 = preg_split("/\*[a-z]/", $mainString[0]);
unset($arr1[0]);
$arr1_values = array_values($arr1);
$arr1_keys = ['name','surname','fathers'];
$result1 = array_combine($arr1_keys, $arr1_values);
// second part of string
$arr2 = preg_split("/\*[a-z]/", $mainString[1]);
unset($arr2[0]);
$arr2_values = array_values($arr2);
$arr2_keys = ['name','surname'];
$arr2 = array_combine($arr2_keys, $arr2_values);
$array_names[] = $arr1;
$array_names[] = $arr2;
return $array_names;
}
// test result !
print_r(splitMyString("*nJohn*sSmith*fGeorge#*nHenry*sFord"));
thanks to all!
For some reason the site blocks my voting for some 'reputation' reason which I find not-fully democracy compliant! On the other hand who cares about democracy these days!
Nevertheless I am using solution #2, without indicating that solution 1 or 3 are not great!
Regards.
However inspired by your answers I came up with mine also, here it is!
$string = '*nJohn*sSmith*fGeorge#*nHenry*sFord';
split_to_key ( $string, array('n'=>'Name','s'=>'Surname','f'=>'Middle'));
function split_to_key ( $string,$ind=array() )
{
$far=null;
$i=0;
$fbig=preg_split('/#/',$string,-1,PREG_SPLIT_NO_EMPTY);
foreach ( $fbig as $fsmall ) {
$f=preg_split('/\*/u',$fsmall,-1,PREG_SPLIT_NO_EMPTY);
foreach ( $f as $fs ) {
foreach( array_keys($ind) as $key ) {
if( preg_match ('/^'.$key.'/u',$fs ) ) {
$fs=preg_replace('/^'.$key.'/u','',$fs);
$far[$i][$ind[$key]]=$fs;
}
}
}
$i++;
}
print_r($far);
}
Like Chris, I wouldn't use preg_split(). My method uses just one preg() function and one loop to completely prepare the filtered output in your desired format (notice my output is 0-indexed, though).
Input (I extended your input sample for testing):
$string='*nJohn*sSmith*fGeorge#*nHenry*sFord#*nJames*sWashington#*nMary*sMiller*fRichard';
Method (PHP Demo & Regex Demo):
if(preg_match_all('/\*n([^*]*)\*s([^*]*)(?:\*f([^#]*))?(?=#|$)/', $string, $out)){
$out=array_slice($out,1); // /prepare for array_column()
foreach($out[0] as $i=>$v){
$name_array[$i]=array_combine(['name','surname','father'],array_column($out,$i));
if($name_array[$i]['father']==''){unset($name_array[$i]['father']);}
}
}
var_export($name_array);
Output:
array (
0 =>
array (
'name' => 'John',
'surname' => 'Smith',
'father' => 'George',
),
1 =>
array (
'name' => 'Henry',
'surname' => 'Ford',
),
2 =>
array (
'name' => 'James',
'surname' => 'Washington',
),
3 =>
array (
'name' => 'Mary',
'surname' => 'Miller',
'father' => 'Richard',
),
)
My regex pattern is optimized for speed by using "negative character classes". I elected to not use the named capture groups because they nearly double the output array size from preg_match_all() and that array requires further preparation anyhow.
I have the following string, eg: 'Hello [owner], we could not contact by phone [phone], it is correct?'.
Regex would like to return in the form of array, all that is within []. Inside the bracket will only alpha characters.
Return:
$array = [
0 => '[owner]',
1 => '[phone]'
];
How should I proceed to have this return in php?
Try:
$text = 'Hello [owner], we could not contact by phone [phone], it is correct?';
preg_match_all("/\[[^\]]*\]/", $text, $matches);
$result = $matches[0];
print_r($result);
Output:
Array
(
[0] => [owner]
[1] => [phone]
)
I'm assuming that the end goal of all of this is that you want to replace the [placeholder]s with some other text, so use preg_replace_callback instead:
<?php
$str = 'Hello [owner], we could not contact by phone [phone], it is correct?';
$fields = [
'owner' => 'pedrosalpr',
'phone' => '5556667777'
];
$str = preg_replace_callback('/\[([^\]]+)\]/', function($matches) use ($fields) {
if (isset($fields[$matches[1]])) {
return $fields[$matches[1]];
}
return $matches[0];
}, $str);
echo $str;
?>
Output:
Hello pedrosalpr, we could not contact by phone 5556667777, it is correct?
I have string like below
["Day1"]["Morning"]=mutton&["Day1"]["Evening"]=Juice&["Day2"]["Morning"]=burger&["Day2"]["Evening"]=pizza
Which i am trying to convert into array like below
$arr["Day1"]["Morning"] = "mutton";
$arr["Day1"]["Evening"] = "Juice";
$arr["Day2"]["morning"] = "burger";
$arr["Day2"]["evening"] = "pizza";
I tried something like below.
$str = '["Day1"]["Morning"]=mutton&["Day1"]["Evening"]=Juice&["Day2"]["Morning"]=burger&["Day2"]["Evening"]=pizza';
$pieces = explode("&", $str);
foreach($pieces as $pie)
{
$arr.$pie;
}
var_dump($arr);
I know above code is really dumb :/ .Is there any proper solution for this ?
You could do like this...
<?php
$str='["Day1"]["Morning"]=mutton&["Day1"]["Evening"]=Juice&["Day2"]["Morning"]=burger&["Day2"]["Evening"]=pizza';
$arr = explode('&',$str);
foreach($arr as $v)
{
$valarr=explode('=',$v);
preg_match_all('/"(.*?)"/', $valarr[0], $matches);
$narr[$matches[1][0]][$matches[1][1]]=$valarr[1];
}
print_r($narr);
OUTPUT :
Array
(
[Day1] => Array
(
[Morning] => mutton
[Evening] => Juice
)
[Day2] => Array
(
[Morning] => burger
[Evening] => pizza
)
)
You could access like echo $arr["Day1"]["Morning"] which prints mutton
Demo
It looks like it could be parsed with parse_str(), but not without some conversion:
$str = '["Day1"]["Morning"]=mutton&["Day1"]["Evening"]=Juice&["Day2"]["Morning"]=burger&["Day2"]["Evening"]=pizza';
parse_str(preg_replace('/(?<=^|&)/', 'x', str_replace('"', '', $str)), $a);
var_dump($a['x']);
It removes the double quotes, prefixes each entry with x and then applies parse_str(). To get an idea of what the preg_replace() does, the intermediate result is this:
x[Day1][Morning]=mutton&x[Day1][Evening]=Juice&x[Day2][Morning]=burger&x[Day2][Evening]=pizza
Parsing the above string yields an array with a single root element x.
I have an string (like one.two.three or month.2) and I need to translate this string exploding point character into array keys.
So I have month.2 and I need the codeline to translate this string into $lang['month'][2]
I was looking for that solution but I am not able to find it, I am blocked.
I finally found this:
<?php
$lang['one']['two']['three'] = 'well done';
$str = 'one.two.three';
$list = explode('.', $str);
$result = '$lang';
foreach ($list as $item)
{
$result .= '["'.$item.'"]';
}
var_dump(eval("return " . $result.';'));
?>
Start by exploding it:
$in = explode('.', $in);
Then rewrite it as a nested array:
$arg = array();
foreach (array_reverse($in) as $key) {
$arg = array($key => $arg);
}
And finally merge:
$out = array_merge_recursive($arg, $out);
Edit: in case you're seeking to read the array instead, then bazmegakapa's answer is what you're looking for.
I wrote a little recursive function to handle this. First you explode the string, and pass the array ($lang in your example, $a in mine) and this exploded array to GetVal(). It will do its best to return the needed value into $x.
$s="month.2";
$keys=explode('.', $s);
$a=array(
'month' => array('1' => 'fos', '2' => 'fos2'),
'retek' => 1
);
function GetVal($array, $keyarray) {
$key=array_shift($keyarray);
if (array_key_exists($key, $array)) {
if (count($keyarray)==0) {
return $array[$key];
} else {
return GetVal($array[$key], $keyarray);
}
} else {
return null;
}
}
$x=GetVal($a, $keys);
var_dump($x);
Its a clarification for the use of explode function:
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
this code explodes as:
Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
not as Array[hello][world][its][a].......