RE: [PHP] Odd Strpos Behavior
| From: | John W. Holmes | Date: | Tue, 10 Dec 2002 00:20:38 +0000 |
| Subject: | RE: [PHP] Odd Strpos Behavior | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-127582@lists.php.net to get a copy of this message | ||
> I'm getting a really weird return from Strpos. What I'm doing is this,
and
> anyone familiar with any of the table-runner programs for RPG's will
know
> what I'm getting at here, I have a fields, like [adjective], [noun],
etc.,
> which I need to pull out and replace with values from included php
files.
> Ok, so I have a string with those fields in it, and I have a loop that
> scans through the string for the first occurence of ]. On the first
pass,
> strpos returns the first instance of where ] shows up in the string.
> Beautiful. I cut out the field, I create a filename, and it works just
> fine.
It'd be easier to use a regular expression for something like this.
Something like this would work (from PHP Architect):
$s = "I am going to be {a} years old on the {b}th of November, {c}.";
$a = array(
"a" => "one hundred",
"b" => "seventeen",
"c" => "two thousand two");
$z = preg_replace("/\{([a-z]+)\}/e","\$a['$1']",$s);
echo $z;
Output:
I am going to be one hundred years old on the seventeenth of November,
two thousand two.
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/