English 中文(简体)
Get position of all matches in group
原标题:

Consider the following example:

$target =  Xa,a,aX ;
$pattern =  /X((a),?)*X/ ;
$matches = array();
preg_match_all($pattern,$target,$matches,PREG_OFFSET_CAPTURE|PREG_PATTERN_ORDER);
var_dump($matches);

What it does is returning only the last a in the series, but what I need is all the a s.

Particularly, I need the position of ALL EACH OF the a s inside the string separately, thus PREG_OFFSET_CAPTURE.

The example is much more complex, see the related question: pattern matching an array, not their elements per se

Thanks

最佳回答

It groups a single match since the regex X((a),?)*X matches the entire string. The last ((a),?) will be grouped.

What you want to match is an a that has an X before it (and the start of the string), has a comma ahead of it, or has an X ahead of it (and the end of the string).

$target =  Xa,a,aX ;
$pattern =  /(?<=^X)a|a(?=X$|,)/ ;
preg_match_all($pattern, $target, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

Output:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] => 1
                )

            [1] => Array
                (
                    [0] => a
                    [1] => 3
                )

            [2] => Array
                (
                    [0] => a
                    [1] => 5
                )

        )

)
问题回答

When your regex includes X, it matches once. It finds one large match with groups in it. What you want is many matches, each with its own position.

So, in my opinion the best you can do is simply search for /a/ or /a,?/ without any X. Then matches[0] will contain all appearances of a

If you need them between X, pre-select this part of the string.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签