Re: Emulating sprintf???
| From: | Michal Migurski | Date: | Fri, 09 Jul 2004 02:12:41 +0000 |
| Subject: | Re: Emulating sprintf??? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-190017@lists.php.net to get a copy of this message | ||
> Im trying to do something like sprintf() because I would like to have
> control over placeholders. The code I've posted works for the most part,
> but when I introduce content that has the same characters as my
> placeholders (%s and %i), it breaks to hell... Anyone got any ideas on
> how to fix, or tell me Im barking up the wrong tree...
After the first call to substr_replace, you swap in some replacement text
that contains an '%s'. Subsequent calls to substr_replace use your
replacement text as the key, because it's no longer possible to uniquely
determine what was part of the original string, and what was swapped in.
Recommendation: don't use preg_match. You can try using preg_replace with
arrays as arguments, but you may come closer to sprintf's correct behavior
if you instead consume the pattern string ($sql) character by character,
generating an output string made up of characters from the pattern string,
and elements array_shifted off of $data as you encounter placeholders.
Don't forget to add a placeholder that allows you to use '%' in that
string -- sprintf uses '%%'.
Much easier recommendation: use provided sprintf, but parse out any
unwanted placeholders first, using preg_replace.
---------------------------------------------------------------------
michal migurski- contact info and pgp key:
sf/ca http://mike.teczno.com/contact.html