This question already has answers here:
Preg_match_all returning array within array?
(2 answers)
Closed 8 years ago.
I have this:
$text = $_POST['text'];
When I echo $text I get this:
ggg #hhh #ddd ggg hhhrr ggg #ttt
When I do this:
$val = preg_match_all("/#\w+/", $text, $matches);
print_r($matches);
I get
Array ( [0] => Array ( [0] => #hhh [1] => #ddd [2] => #ttt ) )
But I want output like this:
Array ( [0] => #hhh [1] => #ddd [2] => #ttt )
thanks
Another approach is to use named groups.
$val = preg_match_all("/(?P<myLabel>#\w+)/", $text, $matches);
print_r($matches['myLabel']);
Take the first (zeroth) item from the array by changing this:
print_r($matches);
To this:
print_r($matches[0]);
Related
I have a dataset and I want to conevert it into an array and I just can't figure out how...
I've tried a couple things like preg_replace() with regex and explode() but it doesn't come out the way I need it.
So my dataset looks like this:
dataCrossID=12345, DeviceID=[ID=1234567]
dataCrossID=5678, DeviceID=[ID=7654321]
dataCrossID=67899, DeviceID=[ID=87654321]
and the Array should look like this:
$dataSet(
[12345] => 1234567,
[5678] => 7654321,
[67899] => 87654321,
)
I tried regex but the fact that the numbers got different lenghts makes it hard for me.
Does anyone have an idea?
The easiest way would be using preg_match_all with an simple regular expression.
$data = 'dataCrossID=12345, DeviceID=[ID=1234567]
dataCrossID=5678, DeviceID=[ID=7654321]
dataCrossID=67899, DeviceID=[ID=87654321]';
preg_match_all('/=([0-9]+).*=([0-9]+)/', $data, $matches, PREG_SET_ORDER);
$dataSet = [];
foreach ($matches as $match) {
$dataSet[$match[1]] = $match[2];
}
print_r($dataSet);
Use preg_match_all() to identify the pieces of text you need:
$input = <<< E
dataCrossID=12345, DeviceID=[ID=1234567]
dataCrossID=5678, DeviceID=[ID=7654321]
dataCrossID=67899, DeviceID=[ID=87654321]
E;
preg_match_all('/dataCrossID=(\d+), DeviceID=\[ID=(\d+)\]/', $input, $matches, PREG_SET_ORDER);
print_r($matches);
The content of $matches is:
Array
(
[0] => Array
(
[0] => dataCrossID=12345, DeviceID=[ID=1234567]
[1] => 12345
[2] => 1234567
)
[1] => Array
(
[0] => dataCrossID=5678, DeviceID=[ID=7654321]
[1] => 5678
[2] => 7654321
)
[2] => Array
(
[0] => dataCrossID=67899, DeviceID=[ID=87654321]
[1] => 67899
[2] => 87654321
)
)
You can now iterate over $matches and use the values at positions 1 and 2 as keys and values to extract the data into the desired array:
$output = array_reduce(
$matches,
function(array $c, array $m) {
$c[$m[1]] = $m[2];
return $c;
},
array()
);
print_r($output);
The output is:
Array
(
[12345] => 1234567
[5678] => 7654321
[67899] => 87654321
)
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
Trying to capture all text between tags.
Code:
$test = '<test>foo<tests> asdlkfjklas lkflsdakj <test>sdfsd<tests> asdlkaskl <test>235234<tests>';
$match = '/<test>(.*)<tests>/';
preg_match_all($match, $test, $nextLink);
Result of print_r:
Array ( [0] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) [1] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) )
your regex syntax is greedy. use folowing:
$match = '/<test>(.*?)<tests>/';
This question already has answers here:
PHP case-insensitive explode()
(3 answers)
Closed 6 years ago.
I have the following code:
$string = "zero Or one OR two or three";
print_r(explode("or", $string));
Right now this results in:
Array ( [0] => zero Or one OR two [1] => three )
But I want to ignore the case of the delimiter so it works with Or, OR, ... and that my result is:
Array ( [0] => zero [1] => one [2] => two [3] => three )
How can I do this?
Use preg_split()
$string = "zero Or one OR two or three";
$keywords = preg_split("/or/i", $string);
echo '<pre>';print_r($keywords);echo '</pre>';
Outputs:
Array
(
[0] => zero
[1] => one
[2] => two
[3] => three
)
Sorry for dropping an other preg_match regular expression question. I have been dealing with this issue for too long and can't get it to work, so any help is appreciated!!
I need a simple preg_match_all to return me placeholder for the format [#varname].
in a string
"hello, this is [#firstname], i am [#age] years old"
I would expect an array with the values 'firstname' and 'age'.
my attempt for [varname] that worked was:
preg_match_all("/[([^]]+)]/", $t, $result);
anything i tried to get the #character included failed...
preg_match_all("/[\#([^]]+)]/", $t, $result);
Code:
$str = 'hello, this is [#firstname], i am [#age] years old';
preg_match_all('~\[#(.+?)\]~', $str, $matches);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => [#firstname]
[1] => [#age]
)
[1] => Array
(
[0] => firstname
[1] => age
)
)
<?php
$string = "hello, this is [#firstname], i am [#age] years old";
$pattern = '#\[#(.*?)\]#';
preg_match_all($pattern, $string, $matches);
print_r($matches[1]);
?>
Array
(
[0] => firstname
[1] => age
)
This question already has answers here:
Explode string by one or more spaces or tabs
(8 answers)
Closed 7 months ago.
With the following string:
$str = '["one","two"],a,["three","four"],a,,a,["five","six"]';
preg_split( delimiter pattern, $str );
How would I have to set up the delimiter pattern to obtain this result:
$arr[0] = '["one","two"]';
$arr[1] = '["three","four"]';
$arr[2] = '["five","six"]';
In other words, is there a way to split at the pattern ',a,' AND ',a,,a,' BUT check for ',a,,a,' first because ',a,' is a sub string of ',a,,a,'?
Thanks in advance!
If it can only be ,a, and ,a,,a,, then this should be enough:
preg_split("/(,a,)+/", $str);
It looks like what you're actually trying to do is separate out the square bracketed parts. You could do that like so:
$arr = preg_split("/(?<=\])[^[]*(?=\[)/",$str);
Take a look at this code:
$result = array();
preg_match_all("/(\[[^\]]*\])/", '["one","two"],a,["three","four"],a,,a,["five","six"]', $result);
echo '<pre>' . print_r($result, true);
It will return:
Array
(
[0] => Array
(
[0] => ["one","two"]
[1] => ["three","four"]
[2] => ["five","six"]
)
[1] => Array
(
[0] => ["one","two"]
[1] => ["three","four"]
[2] => ["five","six"]
)
)