0

I need to let the user specify a custom format for a function which uses vsprintf, and since PHP doesn't have glibc' register_printf_function(), I'll have to do it with PCRE.

My question is, what would be the best REGEXP to match % followed by any character and not having % before it, in an usable manner for programmatic use afterwards?

The closest solution I could get was:

<?php

function myprintf($format,$args) {
 $matches = array();
 preg_match_all('/((?<!%)%*[^%]+)/', $format,$matches,PREG_OFFSET_CAPTURE|PREG_PATTERN_ORDER);
 print_r($matches);
}

myprintf("begin%a%%b%%%c%d",NULL);

Which kinda works, BUT this gets "confused" by inputs like "%%%c". I would like to have series of two %-signs (that is, escaped) in one grouping, like:

Array (
 0 => '%%',
 1 => '%c'
)

and not like it's doing it now: Array ( 0 => '%%%c' ) That is, I need to keep the input intact, though tokenized, in order to join the pieces together after I do the processing of the custom printf formats I encounter in the input.

Thanks,

Flavius

PS: the "user" is actually another programmer. I am aware of the security implications.

5
  • I think you will end up saner using an ad-hoc parser.
    – Vinko Vrsalovic
    Sep 25, 2009 at 16:12
  • Rather than trying to write a regex that parses printf syntax, why not construct a syntax that is easily parsed by regex? Sep 25, 2009 at 16:25
  • Don't forget that printf also accepts indexed variables a la: "%1$d", "%2$s", etc. Sep 25, 2009 at 16:32
  • I won't. I'm introducing only simple placeholders like "%G", without specifiers. The rest of them will be passed over to vsprintf(), as they are, after joining the pieces together. "%G" will be replaced by another format which is already known to printf and after running it through sprintf() with a variable.
    – Flavius
    Sep 25, 2009 at 16:59
  • Take for instance the need to let the "user" specify where he wants to pad some piece of data with a character. He can supply the data, BUT he doesn't have access to the "padding character" and to how much padding is needed. The padding is decided by my class based on the depth of the tree (yeah, it's a recursive tree and the padding must be done for each node individually, at runtime, using RecursiveIteratorIterator::getDepth().
    – Flavius
    Sep 25, 2009 at 17:01

2 Answers 2

1

Code:

$string = "begin%a%%b%%%c%d";
preg_match_all('/([^%]|%%)+|%.*?[a-zA-Z]/', $string, $matches);
print_r($matches[0]);

Output:

Array
(
    [0] => begin
    [1] => %a
    [2] => %%b%%
    [3] => %c
    [4] => %d
)

This should parse compound format specifiers like %.3f or %$1d properly as well, also.

1

If what you want is a % followed by a letter or another % then you can simply do:

$string = "begin%a%%b%%%c%d";
preg_match_all("/%./", $string, $matches);
$values = $matches[0];

// $values = array(5) { [0]=> string(2) "%a" [1]=> string(2) "%%" [2]=> string(2) "%%" [3]=> string(2) "%c" [4]=> string(2) "%d" }

// begin %a %% b %% %c %d <- is the string with spaces. 

Edit:

I think this is equivalent to what you want from the comments below:

preg_match_all('/(\s?\w+\s?|%[^%]|%%)/', $string, $matches);
$value = $matches[0];

// $value = array(7) { [0]=> string(5) "begin" [1]=> string(2) "%a" [2]=> string(2) "%%" [3]=> string(1) "b" [4]=> string(2) "%%" [5]=> string(2) "%c" [6]=> string(2) "%d" }

The main difference is that [2]=> string(3) "%%b" becomes [2]=> string(2) "%%" [3]=> string(1) "b" which should give you the same results because the %% would be evaluated as a single % anyways.

7
  • A great idea which should fit my needs. I wonder though if there's a way to preg_match_all() and get the entire input tokenized correctly. Thus I'll vote it up, but not accept yet.
    – Flavius
    Sep 25, 2009 at 17:17
  • What I basically need to tell it could also be something like "an % not preceded by more than another %. If it's not preceded, then match forwards until a % is found. Otherwise start a new match"
    – Flavius
    Sep 25, 2009 at 17:22
  • So what exactly would the output be... based on the example? Would it be array(3) { [0]=> string(2) "%a" [1]=> string(2) "%c" [2]=> string(2) "%d" }
    – null
    Sep 25, 2009 at 17:28
  • the input "begin%a%%b%%%c%d" should be tokenized as: 0 => begin 1 => %a 2 => %%b 3 => %% 4 => %c 5 => %d
    – Flavius
    Sep 25, 2009 at 17:36
  • So basically I don't care about the specifiers or modifiers and not even if they're valid, vsprintf() will take care of that. All I care about is parsing escaped %-characters correctly, and having <some-text> after an unescaped %-char as in "%<some-text>". That's all.
    – Flavius
    Sep 25, 2009 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.