I have a long string like this I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];. Now I just want to get certain words like 'I1','I2','I8','NA1' and so on i.e. words between ':'&';' only ,and store them in array. How to do that efficiently?
I have already tried using preg_split() and it works but giving me wrong output. As shown below.
// $a is the string I want to extract words from
$str = preg_split("/[;:]/", $a);
print_r($str);
The output I am getting is this
Array
(
[0] => I8
[1] => 2
[2] => I1
[3] => 1
[4] => I2
[5] => 2
[6] => I3
[7] => 2
[8] => I4
[9] => 4
[10] =>
)
Array
(
[0] => NA1
[1] => 5
[2] =>
)
Array
(
[0] => IA1
[1] => [1,2,3,4,5]
[2] =>
)
Array
(
[0] => S1
[1] => asadada
[2] =>
)
Array
(
[0] => SA1
[1] => [1,2,3,4,5]
[2] =>
)
But I am expecting 'I8','I1','I2','I3','I4' also in seperated array with position [0]. Any help on how to do this.
You could try something like.
<?php
$str = 'I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];';
preg_match_all('/(?:^|[;:])(\w+)/', $str, $result);
print_r($result[1]); // Matches are here in $result[1]
You can perform a greedy match to match the items between ; and : using preg_match_all()
<?php
$str = 'I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];';
preg_match_all('/;(.+?)\:/',$str,$matches);
print_r($matches[1]);
Live Demo: https://3v4l.org/eBsod
One possible approach is using a combination of explode() and implode(). The result is returned as a string, but you can easily put it into an array for example.
<?php
$input = "I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];.";
$output = array();
$array = explode(";", $input);
foreach($array as $item) {
$output[] = explode(":", $item)[0];
}
echo implode(",", $output);
?>
Output:
I1,I2,I8,NA1,IA1,S1,SA1,SA1,.
For some reason I can't get strpos to work to search my array, even if $jobList[1] and $titlesearch are the same values... Sorry if it's something obvious but I'm still pretty new to coding!
I begin with my $data array which looks like this:
Array
(
[0] => P0001 Lifeguard descexample 18/09/18 parttime fixedterm mail vic
[2] => P0002 IT Manager descexample 18/09/18 fulltime ongoing post mail sa
)
I then explode each of these entries into their own array...
for ($i = 0; $i < count($data); $i++) {
$jobList = explode("\t", $data[$i]);
}
Array
(
[0] => P0001
[1] => Lifeguard
[2] => descexample
[3] => 18/09/18
[4] => parttime
[5] => fixedterm
[6] =>
[7] => mail
[8] => vic
)
Array
(
[0] => P0002
[1] => IT Manager
[2] => descexample
[3] => 18/09/18
[4] => fulltime
[5] => ongoing
[6] => post
[7] => mail
[8] => sa
)
Now I'm trying to search through these arrays from a user input, $titlesearch, and find it's matches with the job titles, $jobList[1]:
if (strpos($jobList[1], $titlesearch)) {
echo "nice one";
}
No matter what loops I try, the strpos never returns true, even if I echo the values and they both give the same result, so I'm really not sure what I'm doing wrong :'(
Any help is greatly appreciated!
You should always compare the data type when using this function as it may not return a boolean and it can be missleading. Check documentation here
Try it something like this:
if (strpos($jobList[1], $titlesearch) !== false) {
echo "nice one";
}
I have a string that I want to convert into an array of 6 elements.
$x=Address : "MK/LK G8, 2ND FLR, MALAL VISO INFO 19-Aug-15 Acct Number : _254566003 etc...
IF I want an array
$parts[0]=MK/LKG......Length should be 6 (whitespace wont consider)
$parts[1]=82NDFLRLength should be 6
$parts[2]=etc.....
try this to split strings to array
$str = "MK/LK G8, 2ND FLR, MALAL VISO INFO 19-Aug-15 Acct Number : _254566003 etc..."
$arr = str_split($str, 6);
Simply use str_split along with the preg_replace as
$x="MK/LK G8, 2ND FLR, MALAL VISO INFO 19-Aug-15 Acct Number : _254566003 etc..";
$res = str_split(preg_replace('/\s/','',$x),6);
print_r($res);
Output:
Array
(
[0] => MK/LKG
[1] => 8,2NDF
[2] => LR,MAL
[3] => ALVISO
[4] => INFO19
[5] => -Aug-1
[6] => 5AcctN
[7] => umber:
[8] => _25456
[9] => 6003et
[10] => c..
)
Demo
$fileNameMatchRegex = ^a-[0-9]*_b-[0-9]*_c-.*(_d-on)?_((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))-[0-9]*\.csv$
$fileNames =
Array
(
[0] => index.html
[1] => a-34234234_b-3271_c-123_d-on_2013-08-12-10.csv
[2] => a-52342345_b-3271_c-123_d-on_2013-08-12-11.csv
[3] => a-8764453_b-3271_c-123_d-on_2013-08-12-12.csv
[4] => a-7654334_b-3271_c-1234_d-on_2013-08-12-4.csv
[5] => a-3435_b-3271_c-23re_d-on_2013-08-12-5.csv
[6] => a-909876_b-3271_c-wef2r2_d-on_2013-08-12-6.csv
[7] => a-345456_b-3271_c-23rwef_d-on_2013-08-12-7.csv
[8] => a-98765_b-3271_c-23ref_d-on_2013-08-12-8.csv
[9] => a-098765_b-3271_c-wef2r_d-on_2013-08-12-9.csv
)
$matchingFileNames = preg_grep ("/".$fileNameMatchRegex."/", $fileNames);
This works here...I get a match on 1-9: regex tester
Its working fine for me
$fileNames = explode(',', "index.html,a-34234234_b-3271_c-123_d-on_2013-08-12-10.csv,a-52342345_b-3271_c-123_d-on_2013-08-12-11.csv,a-8764453_b-3271_c-123_d-on_2013-08-12-12.csv,a-7654334_b-3271_c-1234_d-on_2013-08-12-4.csv,a-3435_b-3271_c-23re_d-on_2013-08-12-5.csv,a-909876_b-3271_c-wef2r2_d-on_2013-08-12-6.csv,a-345456_b-3271_c-23rwef_d-on_2013-08-12-7.csv,a-98765_b-3271_c-23ref_d-on_2013-08-12-8.csv,a-098765_b-3271_c-wef2r_d-on_2013-08-12-9.csv");
$fileNameMatchRegex = '^a-[0-9]*_b-[0-9]*_c-.*(_d-on)?_((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))-[0-9]*\.csv$';
$matchingFileNames = preg_grep ("#".$fileNameMatchRegex."#", $fileNames);
print_r($fileNames);
print_r($matchingFileNames);
but i used '#' around the regexp as delimiter, as your regexp included '/',
you proberly just need to escape your '/' or chose a better delimiter
I'm trying to separate some strings by different criteria but I can't get the desired results.
Here are 3 examples:
$ppl[0] = "Balko, Vlado \"Panelбk\" (2008) {Byt na tretom (#1.55)}";
$ppl[1] = "'Abd Al-Hamid, Ja'far A Two Hour Delay (2001)";
$ppl[2] = "'t Hoen, Frans De reьnie (1963) (TV)";
I'm currently using this for the last 2:
$pattern = '#,|\t|\(#'
But I will get and empty space.
result:
Array ( [0] => 'Abd Al-Hamid [1] => Ja'far [2] => A Two Hour Delay [3] => 2001) )
Array ( [0] => 't Hoen [1] => Frans [2] => [3] => De reünie [4] => 1963) [5] => TV) )
As for the 1st expression I used another pattern but I still get empty spaces. Any ideas?
EDIT:
Thanks this helped indeed. I tried using a modified version on the first string:
$pattern4 = '#[",\t]+|[{}]+|[()]+#';
However I still get an empty space:
Array ( [0] => Balko [1] => Vlado [2] => Panelák [3] => [4] => 2008 [5] => [6] => Byt na tretom [7] => #1.55 [8] => [9] => )
What should I do? I think that the " and the brackets are causing the problem but I don't know how to fix it.
I would surmise you have two tabs as separator in your second and third example string. (Can't see that here, the SO editor converts them into spaces).
But you could adapt your regex slightly in that case:
$pattern = '#,|\t+|\(#'
Or simpler even:
$pattern = '#[,\t(]+#'
And the alternatve, btw, would be just applying array_filter() on the result arrays to remove the empty entries.