Viewed   64 times

I'm trying to get all substrings matched with a multiplier:

$list = '1,2,3,4';
preg_match_all('|d+(,d+)*|', $list, $matches);
print_r($matches);

This example returns, as expected, the last match in [1]:

Array
(
    [0] => Array
        (
            [0] => 1,2,3,4
        )

    [1] => Array
        (
            [0] => ,4
        )

)

However, I would like to get all strings matched by (,d+), to get something like:

Array
(
    [0] => ,2
    [1] => ,3
    [2] => ,4
)

Is there a way to do this with a single function such as preg_match_all()?

 Answers

4

According to Kobi (see comments above):

PHP has no support for captures of the same group

Therefore this question has no solution.

Saturday, September 3, 2022
4

You are missing a curl option for handling HTTPS request that is

CURLOPT_SSL_VERIFYPEER : FALSE to stop cURL from verifying the peer's certificate

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Friday, December 23, 2022
 
ccpizza
 
5

This is how I think I would do it:

<?php

$string = '(((aaa (bbb) ccc)(ddd (eee) fff) ggg)(hhh (iii) )(jjj (kkk) lll) mmm)(nnn (ooo) ppp)(qqq (rrr) sss)';

function parse_string($input) {
    $len = strlen($input);
    $substrings = array();
    $paren_count = 0;
    $cur_string = '';
    for ($i = 0; $i < $len; $i++) {
        $char = $input[$i];
        if ($char == '(') {
            $paren_count += 1;
        } elseif ($char == ')') {
            $paren_count -= 1;
        }
        $cur_string .= $char;
        if ($paren_count == 0 && strlen($cur_string)) {
            $substrings[] = $cur_string;
            $cur_string = '';
        }
    }
    return $substrings;
}

function convert_str($input) {
    $search = array('(', ')', ' ');
    $replace = array('', '', ',');
    return str_replace($search, $replace, $input);
}


$parsed_string = parse_string($string);
echo convert_str($parsed_string[1]);

OUTPUT:

nnn,ooo,ppp

This is a type of state machine.

Wednesday, August 10, 2022
4

It's happening because of this line :

        if(!class_exists($class_name)){
            return 'Error: Content has not been found.';
        }

In the case the class doesn't exist, you're returning a string from the function, and afterwards trying to call a method render() on it. You can fix it either by changing this behaviour (don't return an error string, but use an Exception or trigger_error() ) or checking that the function loadClass() does return a valid object through is_object() for example.

Regarding your second question, you should expand your tests in loadClass() to handle the empty GET variable case, and substitute the empty string with a "Home" default value.


Update :

Example usage for is_object in your case :

$class = loadClass($ConfigPage->getFormVariable('Page'));
if(! is_object($class)) {
    // if $class is not an object, then something went wrong above
    throw new Exception('Invalid data returned from loadClass()');
}
$ConfigPage->SetProperty('content', $class);
Wednesday, November 16, 2022
 
3
preg_match_all('/[startstring](.*?)[endstring]/s', $input, $matches);
echo implode(' ', $matches);

preg_match_all('/[startstring](.*?)=(.*?)[endstring]/s', $input, $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
    $key = $matches[1][$i];
    $value = $matches[2][$i];
    $$key = $value;
}
Friday, October 28, 2022
 
dororo
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :