capturing particular string and values from a php string - php

I have a variable in php that has the following value:
var $line="'PHYSICAL':3.8,'ORGANIZATIONAL':4,'TECHNICAL':2.9";
From that line variable, I want to extract each word and values in php, so basically I want something like below:
var $word1=PHYSICAL;
var $word1value=3.8;
var $word2=ORGANIZATIONAL;
var $word2value=4;
var $word3=TECHNICAL;
var $word3value=2.9;
I want to capture all the words and values separately in different variables, so that later I can process them. Can anyone please assist me on this. Thanks.

The simplest way to do this is with preg_split. You can use the PREG_SPLIT_DELIM_CAPTURE flag to enable capturing the word without the surrounding quotes, and PREG_SPLIT_NO_EMPTY to remove the empty values that arise from this particular split pattern.
$line="'PHYSICAL':3.8,'ORGANIZATIONAL':4,'TECHNICAL':2.9";
$array = preg_split("/'([^']+)':|,/", $line, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($array);
Output:
Array
(
[0] => PHYSICAL
[1] => 3.8
[2] => ORGANIZATIONAL
[3] => 4
[4] => TECHNICAL
[5] => 2.9
)
You can then post-process the array (for example using array_filter and array_combine) to produce something which might be more useful:
$newarray = array_combine(array_filter($array, function ($i) { return !($i % 2); }, ARRAY_FILTER_USE_KEY),
array_filter($array, function ($i) { return $i % 2; }, ARRAY_FILTER_USE_KEY));
print_r($newarray);
Output:
Array
(
[PHYSICAL] => 3.8
[ORGANIZATIONAL] => 4
[TECHNICAL] => 2.9
)
If you really want the individual variables you can do this:
for ($i = 0; $i < count($array); $i += 2) {
${'word' . intdiv($i+2, 2)} = $array[$i];
${'word' . intdiv($i+2, 2) . 'value'} = $array[$i+1];
}
echo "word1 = $word1\n";
echo "word1value = $word1value\n";
echo "word2 = $word2\n";
echo "word2value = $word2value\n";
echo "word3 = $word3\n";
echo "word3value = $word3value\n";
Output:
word1 = PHYSICAL
word1value = 3.8
word2 = ORGANIZATIONAL
word2value = 4
word3 = TECHNICAL
word3value = 2.9

Related

how to store last character of the world from the string into an array in PHP

I have a string like -
$str = "Hello how are you";
and i want to store last character in an array then the result look like below-
array(0=>o,1=>w,2=>e,3=>u)
how It can be achieve without the using of php explode(), substr() and array_split() methods.
This works without any function calls whatsoever (isset is actually a language construct, not a function.)
:
$str = "Hello how are you";
for ($i = 0; isset($str[$i]); $i++) {
if (!isset($str[$i + 1]) || $str[$i + 1] == " ") {
$result[] = $str[$i];
}
}
It addresses each byte of the string one at a time, checking to see if it's the last one or is followed by a space and adding it to the array if so. It's a simplistic set of rules for determining the end of a word, but illustrates the idea.
Verifying with print_r($result) outputs:
Array
(
[0] => o
[1] => w
[2] => e
[3] => u
)
i dunno why am i doing this but get it
$str = "Hello how are you";
$last='';
$i=0;
$result=array();
while ($i<strlen($str)){
if ($str[$i]==' '){
$result[]=$last;
}
$last= $str[$i];
if ($i==(strlen($str)-1)){
$result[]=$last;
}
$i++;
}
print_r($result);
The alternative solution using str_word_count function:
$str = "Hello how are you";
$last_chars = [];
foreach (str_word_count($str, 1) as $word) {
$last_chars[] = $word[strlen($word) - 1];
}
print_r($last_chars);
The output:
Array
(
[0] => o
[1] => w
[2] => e
[3] => u
)
As you mention you don't want to use explode, substr and split, so for now you need to use strlen for getting the string length.
After getting the array length you need to loop through the length and check if the character is a blank space or last character. If then add the previous character to the array of output.
$str = "Hello how are you";
$length = strlen($str);
$out = array();
for($i = 0; $i < $length; $i++){
if($str[$i] == " " || ($i == $length - 1)){
$out[] = $str[$i-1];
}
}
print_r($out);
Result
Array
(
[0] => o
[1] => w
[2] => e
[3] => u
)

PHP - Checking up on string with regex and push to array

I have two variables as follows:
$string1 = '/test/10/25';
$string2 = '/test/[0-9]+/[0-9]+
Is it possible to make PHP compare these two strings and push the actual ID's (10 and 25) into an array like so by using the regex as some sort of guidance?
Array
(
[0] => 10
[1] => 25
)
I tried playing around with preg_match() but this just puts everything into the same array key.
This will work for you
<?php
$string1 = '/test/10/25';
$string2 = '/\/test\/([0-9]+)\/([0-9]+)/';
$matches = [];
preg_match($string2, $string1, $matches);
$array = [];
for($i = 1; $i < count($matches); $i ++)
{
array_push($array, $matches[$i]);
}
print_r($array);
$array will have the matches inside. I had to change the regex string because it was not "valid" for php.

Count delimiter(s) of a string [duplicate]

What is the most efficient way to count all the occurrences of a specific character in a PHP string?
use this:
echo substr_count("abca", "a"); // will echo 2
Can you not feed the character to preg_match_all?
Not sure what kind of a response you're looking for, but here's a function that might do it:
function findChar($c, $str) {
indexes = array();
for($i=0; $i<strlen($str); $i++) {
if ($str{$i}==$c) $indexes[] = $i;
}
return $indexes;
}
Pass it the character you're looking for and the string you want to look:
$mystring = "She shells out C# code on the sea shore";
$mychar = "s";
$myindexes = $findChar($mychar, $mystring);
print_r($myindexes);
It should give you something like
Array (
[0] => 0
[1] => 4
[2] => 9
[3] => 31
[4] => 35
)
or something...
If you are going to be repeatedly checking the same string, it'd be smart to have some sort of trie or even assoc array for it otherwise, the straightforward way to do it is...
for($i = 0; $i < strlen($s); $i++)
if($s[i] == $c)
echo "{$s[i]} at position $i";

Put number separately in array

I would like to put a number separately in an array
ex.
$num = 345;
should be in an array so i can call the numbers as
$num[1] (which should return 4)
I tried str_split($num,1) but without succes.
Thanks
EDIT -------
After some more research str_split($num,1) actually did the trick.
(thanks, Crayon Violent)
$num = 345;
$arr1 = str_split($num); print_r($arr1); //Array ( [0] => 3 1 => 4
[2] => 5 )
echo $arr11; //4
str-split
If you are just trying to get individual characters from the string, use substr.
$second_digit = substr( $num, 1, 1 );
while ($num >0) {
$arr[i] = $num %10;
$num = $num/10;
i++
}
//this leaves the array in reverse order i.e. 543
//to flip
array_reverse($arr);

Need Help in building a logic

I need to get all the positions of a character in a string in a form of an array. I know about the php function strpos() but it does not accept an array as an argument.
This is required:
$name = "australia"; //string that needs to be searched
$positions_to_find_for = "a"; // Find all positions of character "a" in an array
$positions_array = [0,5,8]; // This should be the output that says character "a" comes at positions 0, 5 and 8 in string "australia"
Question: What Loops can help me build a function that can help me achieve the required output?
You can use a for to loop that string:
$name = "australia";
$container = array();
$search = 'a';
for($i=0; $i<strlen($name); $i++){
if($name[$i] == $search) $container[] = $i;
}
print_r($container);
/*
Array
(
[0] => 0
[1] => 5
[2] => 8
)
*/
Codepad Example
No loops necessary
$str = 'australia';
$letter='a';
$letterPositions = array_keys(
array_intersect(
str_split($str),
array($letter)
)
);
var_dump($letterPositions);

Categories