Reputation: 7594
In PHP I'm trying to match each character as its own group. Which would mimic the str_split()
. I tried:
$string = '123abc456def';
preg_match_all('/(.)*/', $string, $array);
// $array = array(2) {
// [0]=> array(2) {
// [0]=> string(12) "123abc456def"
// [1]=> string(0) "" }
// [1]=> array(2) { [0]=> string(1) "f" [1]=> string(0) "" } }
I was expecting something like:
//$array = array(2) {
// [0]=> string(12) "123abc456def",
// [1]=> array(12) {
// [0]=> string(1) "1", [1]=> string(1) "2"
// ...
// [10]=> string(1) "e", [11]=> string(1) "f" } }
The reason I want to use the regular expression instead of a str_split()
is because the regex will be the basis of another regex.
Upvotes: 0
Views: 774
Reputation: 5316
i don't know if this helps for this station but you can access letters of a string like array.. so
<?php
$a = "hede";
print $a[0] . "\n";
print $a[1] . "\n";
print $a[2] . "\n";
print $a[3] . "\n";
?>
will output
h
e
d
e
Upvotes: 0
Reputation: 655239
Try this:
preg_match_all('/./s', $str, $matches)
This does also match line break characters.
Upvotes: 2
Reputation: 556
Maybe this is what you are looking for:
preg_match_all('/(.)+?/', $string, $array);
Upvotes: 0
Reputation: 12900
The * outside the parens means you want to repeat the capturing group. (This means you will only capture the last iteration.) Try a global match of any single character, like this:
preg_match_all('/(.)/', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
Upvotes: 4