RE: [PHP] Odd Strpos Behavior

2002-12-16 Thread Steve Keller
At 12/14/2002 12:50 AM, John W. Holmes wrote:

> And here's a good example of why you should always test each solution
> and time it to see what's better. I was recommending a
> preg_replace_callback solution which I thought, as a lot of other people
> would also think, is a lot faster that your own "method" of doing it.

Thanks John, for both the time tests and for fixing my original method. I 
appreciate the help.
--
S. Keller
UI Engineer
The Health TV Channel, Inc.
(a non - profit organization)
3820 Lake Otis Pkwy.
Anchorage, AK 99508
907.770.6200 ext.220
907.336.6205 (fax)
Email: [EMAIL PROTECTED]
Web: www.healthtvchannel.org


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Odd Strpos Behavior

2002-12-13 Thread John W. Holmes
And here's a good example of why you should always test each solution
and time it to see what's better. I was recommending a
preg_replace_callback solution which I thought, as a lot of other people
would also think, is a lot faster that your own "method" of doing it.
So, I wrote the preg_replace_callback method and timed it against your
method using the following input and replacement strings:

//test string
$string = "This is a [test] of something [that] will hopefully [work].";

//replacement strings
$test = "test_one, test_two [foo], test_three";
$that = "that_one, that_two, that_three [foo]";
$work = "work_one [foo], work_two, work_three";
$foo = "foo_one [test], foo_two [that], foo_three [work]";

That's actually some complex replacement because [foo] can be replaced
with a replacement that refers back to [test] which refers again to
[foo], etc... So actually each run will result in a different string
entirely and would take different amounts of times based on what was
randomly chosen as a replacement. So, I ran each method 1000 times to
kind of equal out the randomness...

Anyway, here are the results. The test file I used is also attached. You
may be able to tweak it further, I don't know. But, since you're opening
and reading files, the small difference between the two may be negated
by having to deal with files... always test your solutions.

Original String: This is a [test] of something [that] will hopefully
[work].
Replaced String: This is a test_two foo_one test_two foo_two that_one of
something that_two will hopefully work_three.
Preg_replace_callback Time: 0.226889014244


Original String: This is a [test] of something [that] will hopefully
[work].
Replaced String: This is a test_two foo_one test_three of something
that_two will hopefully work_two.
String-parser Time: 0.187005996704

(The "replaced string" shown is just the last result of running it 1000
times)

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/

> -Original Message-
> From: Steve Keller [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, December 12, 2002 5:09 PM
> To: [EMAIL PROTECTED]
> Subject: RE: [PHP] Odd Strpos Behavior
> 
> At 12/11/2002 08:09 PM, you wrote:
> 
> >Okay, so how do you know what to replace something like [author]
with?
> >What exactly are you doing again? I've forgotten the original
question.
> >:)
> 
> Ok, got a sentence, like:
> 
>  a pile of [metal] 600 feet wide and 30 feet tall. On top of
it is
> a [monster].
> 
> The items in the [] represent the names of files containing lists of
> possible results. So I need to grab the first one, "metal," open up
> "metal.php," grab a random item from it, such as "gold," and replace
> [metal] in the original sentence with the result. I should now have:
> 
>  a pile of gold 600 feet wide and 30 feet tall. On top of it
is a
> [monster].
> 
> Now, what I was doing before was strpos'ing the []'s and grabbing what
as
> in between to be the file name, then repeating in a loop until I've
> eliminated all of the [] items. The reason I'm doing it in a loop is
> because the results of the "metal.php" random item may include their
own
> []
> items, which also need to be run exactly the same way. A result might
be
> "gold [objects]" and then I have to run through the objects.php file
and
> get a result from that.
> 
> This is what I started with:
> 
>   $a = "[adjective] [beginning]"; // temporary item for testing
> 
>  $e = strpos($a,"]");
>  while ($e) {
>  echo "A: ".$a."";
>  echo "E: ".$e."";
>  $f = strpos($a, "[");
>  echo "F: ".$f."";
>  $tmp = substr($a, 0, $f);
>  $table=substr($a, $f+1, $e-1);
>  echo "Table: ".$table."";
>  $a = substr($a, $e+1, strlen($a));
>  $dataFile = $table.".php";
>  //$b = getFileElement($dataFile);
>  $tmp .= $b;
>  $tmp .= $a;
>  $a = $tmp;
>  $e = strpos($a,"]");
>  }
>  echo $a;
> ?>
> 
> That should work just fine. I cut out everything up to the first [ and
add
> it to $tmp. Then I get the next sequence of characters up to the first
]
> and use it as a file name for the "getFileElement&quo

RE: [PHP] Odd Strpos Behavior

2002-12-13 Thread John W. Holmes
> >Okay, so how do you know what to replace something like [author]
with?
> >What exactly are you doing again? I've forgotten the original
question.
> >:)
> 
> Ok, got a sentence, like:
> 
>  a pile of [metal] 600 feet wide and 30 feet tall. On top of
it is
> a [monster].
> 
> The items in the [] represent the names of files containing lists of
> possible results. So I need to grab the first one, "metal," open up
> "metal.php," grab a random item from it, such as "gold," and replace
> [metal] in the original sentence with the result. I should now have:
> 
>  a pile of gold 600 feet wide and 30 feet tall. On top of it
is a
> [monster].
> 
> Now, what I was doing before was strpos'ing the []'s and grabbing what
as
> in between to be the file name, then repeating in a loop until I've
> eliminated all of the [] items. The reason I'm doing it in a loop is
> because the results of the "metal.php" random item may include their
own
> []
> items, which also need to be run exactly the same way. A result might
be
> "gold [objects]" and then I have to run through the objects.php file
and
> get a result from that.
> 
> This is what I started with:
> 
>   $a = "[adjective] [beginning]"; // temporary item for testing
> 
>  $e = strpos($a,"]");
>  while ($e) {
>  echo "A: ".$a."";
>  echo "E: ".$e."";
>  $f = strpos($a, "[");
>  echo "F: ".$f."";
>  $tmp = substr($a, 0, $f);
>  $table=substr($a, $f+1, $e-1);
>  echo "Table: ".$table."";
>  $a = substr($a, $e+1, strlen($a));
>  $dataFile = $table.".php";
>  //$b = getFileElement($dataFile);
>  $tmp .= $b;
>  $tmp .= $a;
>  $a = $tmp;
>  $e = strpos($a,"]");
>  }
>  echo $a;
> ?>
> 
> That should work just fine. I cut out everything up to the first [ and
add
> it to $tmp. Then I get the next sequence of characters up to the first
]
> and use it as a file name for the "getFileElement" function. Add the
> result
> to $tmp, add what was left after the first ], and viola.
> 
> My problem is that, on the first loop through, strpos returns exactly
> where
> the first ] is, so I can chop up to that no problem. However, the
second
> time through the loop, it's one off, which breaks the logic of the
loop, I
> end up with "beginning]" as my file name.

Here's your fix for the method you are using now, btw:

Change:
$table=substr($a, $f+1, $e-1);
to:
$table=substr($a, $f+1, $e-$f-1);

$f is position of first [, $e is position of first ]. So, you don't want
a substr length of $e-1 (which is from the beginning of the string, you
want the length to be the difference of $f from $e. Hope that helps.

Your original code was only working for the first one because you
started of with a [word]. If you put text before the first [word], it
fails on all accounts. 

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Odd Strpos Behavior

2002-12-13 Thread 1LT John W. Holmes
Do you really want to pursue the solution you're using? A solution with
preg_replace_callback() would probably be a lot faster and easier to manage.
If that doesn't matter, just let me know and I'll look over your code to try
and figure out what's going on. I don't have time right now, though, to do
it.

---John Holmes...

- Original Message -
From: "Steve Keller" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, December 12, 2002 5:09 PM
Subject: RE: [PHP] Odd Strpos Behavior


> At 12/11/2002 08:09 PM, you wrote:
>
> >Okay, so how do you know what to replace something like [author] with?
> >What exactly are you doing again? I've forgotten the original question.
> >:)
>
> Ok, got a sentence, like:
>
>  a pile of [metal] 600 feet wide and 30 feet tall. On top of it is
> a [monster].
>
> The items in the [] represent the names of files containing lists of
> possible results. So I need to grab the first one, "metal," open up
> "metal.php," grab a random item from it, such as "gold," and replace
> [metal] in the original sentence with the result. I should now have:
>
>  a pile of gold 600 feet wide and 30 feet tall. On top of it is a
> [monster].
>
> Now, what I was doing before was strpos'ing the []'s and grabbing what as
> in between to be the file name, then repeating in a loop until I've
> eliminated all of the [] items. The reason I'm doing it in a loop is
> because the results of the "metal.php" random item may include their own
[]
> items, which also need to be run exactly the same way. A result might be
> "gold [objects]" and then I have to run through the objects.php file and
> get a result from that.
>
> This is what I started with:
>
>   $a = "[adjective] [beginning]"; // temporary item for testing
>
>  $e = strpos($a,"]");
>  while ($e) {
>  echo "A: ".$a."";
>  echo "E: ".$e."";
>  $f = strpos($a, "[");
>  echo "F: ".$f."";
>  $tmp = substr($a, 0, $f);
>  $table=substr($a, $f+1, $e-1);
>  echo "Table: ".$table."";
>  $a = substr($a, $e+1, strlen($a));
>  $dataFile = $table.".php";
>  //$b = getFileElement($dataFile);
>  $tmp .= $b;
>  $tmp .= $a;
>  $a = $tmp;
>  $e = strpos($a,"]");
>  }
>  echo $a;
> ?>
>
> That should work just fine. I cut out everything up to the first [ and add
> it to $tmp. Then I get the next sequence of characters up to the first ]
> and use it as a file name for the "getFileElement" function. Add the
result
> to $tmp, add what was left after the first ], and viola.
>
> My problem is that, on the first loop through, strpos returns exactly
where
> the first ] is, so I can chop up to that no problem. However, the second
> time through the loop, it's one off, which breaks the logic of the loop, I
> end up with "beginning]" as my file name.
> --
> S. Keller
> UI Engineer
> The Health TV Channel, Inc.
> (a non - profit organization)
> 3820 Lake Otis Pkwy.
> Anchorage, AK 99508
> 907.770.6200 ext.220
> 907.336.6205 (fax)
> Email: [EMAIL PROTECTED]
> Web: www.healthtvchannel.org
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Odd Strpos Behavior

2002-12-13 Thread Ford, Mike [LSS]
> -Original Message-
> From: Steve Keller [mailto:[EMAIL PROTECTED]]
> Sent: 12 December 2002 22:09
> 
> At 12/11/2002 08:09 PM, you wrote:
> 
> >Okay, so how do you know what to replace something like 
> [author] with?
> >What exactly are you doing again? I've forgotten the 
> original question.
> >:)
> 
> Ok, got a sentence, like:
> 
>  a pile of [metal] 600 feet wide and 30 feet tall. On 
> top of it is 
> a [monster].
> 
> The items in the [] represent the names of files containing lists of 
> possible results. So I need to grab the first one, "metal," open up 
> "metal.php," grab a random item from it, such as "gold," and replace 
> [metal] in the original sentence with the result. I should now have:
> 
>  a pile of gold 600 feet wide and 30 feet tall. On 
> top of it is a 
> [monster].

Well, I think I'd approach this using preg_match_all() to break out all your
elements for replacement into an array, then look them up and produce a
second array of the desired replacements, and then do a preg_replace using
those two arrays to do your desired replaces.

Something like (completely untested!):

$matches = preg_match_all('|\[(.*\)]|U', $sentence, $items);

if ($matches):
$replacements = array();
   foreach ($items[1] as $i=>$item):
  $replacements[$i] = /* random item looked up in "$item.php" */;
  $items[1][$i] = '|\['.$item.'\]|'; // reconstruct as valid preg
pattern
   endforeach;
   $new_sentence = preg_replace($items[1], $replacements, $sentence);
endif;

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Odd Strpos Behavior

2002-12-12 Thread Steve Keller
At 12/11/2002 08:09 PM, you wrote:


Okay, so how do you know what to replace something like [author] with?
What exactly are you doing again? I've forgotten the original question.
:)


Ok, got a sentence, like:

a pile of [metal] 600 feet wide and 30 feet tall. On top of it is 
a [monster].

The items in the [] represent the names of files containing lists of 
possible results. So I need to grab the first one, "metal," open up 
"metal.php," grab a random item from it, such as "gold," and replace 
[metal] in the original sentence with the result. I should now have:

a pile of gold 600 feet wide and 30 feet tall. On top of it is a 
[monster].

Now, what I was doing before was strpos'ing the []'s and grabbing what as 
in between to be the file name, then repeating in a loop until I've 
eliminated all of the [] items. The reason I'm doing it in a loop is 
because the results of the "metal.php" random item may include their own [] 
items, which also need to be run exactly the same way. A result might be 
"gold [objects]" and then I have to run through the objects.php file and 
get a result from that.

This is what I started with:


$a = "[adjective] [beginning]"; // temporary item for testing

$e = strpos($a,"]");
while ($e) {
echo "A: ".$a."";
echo "E: ".$e."";
$f = strpos($a, "[");
echo "F: ".$f."";
$tmp = substr($a, 0, $f);
$table=substr($a, $f+1, $e-1);
echo "Table: ".$table."";
$a = substr($a, $e+1, strlen($a));
$dataFile = $table.".php";
//$b = getFileElement($dataFile);
$tmp .= $b;
$tmp .= $a;
$a = $tmp;
$e = strpos($a,"]");
}
echo $a;
?>

That should work just fine. I cut out everything up to the first [ and add 
it to $tmp. Then I get the next sequence of characters up to the first ] 
and use it as a file name for the "getFileElement" function. Add the result 
to $tmp, add what was left after the first ], and viola.

My problem is that, on the first loop through, strpos returns exactly where 
the first ] is, so I can chop up to that no problem. However, the second 
time through the loop, it's one off, which breaks the logic of the loop, I 
end up with "beginning]" as my file name.
--
S. Keller
UI Engineer
The Health TV Channel, Inc.
(a non - profit organization)
3820 Lake Otis Pkwy.
Anchorage, AK 99508
907.770.6200 ext.220
907.336.6205 (fax)
Email: [EMAIL PROTECTED]
Web: www.healthtvchannel.org


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Odd Strpos Behavior

2002-12-11 Thread John W. Holmes
> >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.
> 
> Now that I've been shown what you meant, and had a chance to play with
it,
> I can see that it's not really going to work out for me. The reason
is, I
> don't know in advance what the contents of the [] will be.

Okay, so how do you know what to replace something like [author] with?
What exactly are you doing again? I've forgotten the original question.
:)

I'm sure there is a better, faster way to do it with regular expressions
or something similar that going digit by digit and looking for [ and ],
etc...

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Odd Strpos Behavior

2002-12-11 Thread Steve Keller
At 12/9/2002 07:20 PM, you wrote:


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.


Now that I've been shown what you meant, and had a chance to play with it, 
I can see that it's not really going to work out for me. The reason is, I 
don't know in advance what the contents of the [] will be.
--
S. Keller
UI Engineer
The Health TV Channel, Inc.
(a non - profit organization)
3820 Lake Otis Pkwy.
Anchorage, AK 99508
907.770.6200 ext.220
907.336.6205 (fax)
Email: [EMAIL PROTECTED]
Web: www.healthtvchannel.org


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: Fwd: RE: [PHP] Odd Strpos Behavior

2002-12-11 Thread Steve Keller
At 12/10/2002 03:43 PM, you wrote:


John's suggestion of using '{', and '}' as the tag delimiters was to simplify
the regex. You can continue to use '[', and ']' as your delimiters, just
change the regex accordingly -- and don't forget that '[', and ']' needs to
be escaped.


Ah, all right. That went completely over my head when he suggested it. 
Thanks for clarifying.
--
S. Keller
UI Engineer
The Health TV Channel, Inc.
(a non - profit organization)
3820 Lake Otis Pkwy.
Anchorage, AK 99508
907.770.6200 ext.220
907.336.6205 (fax)
Email: [EMAIL PROTECTED]
Web: www.healthtvchannel.org


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: Fwd: RE: [PHP] Odd Strpos Behavior

2002-12-09 Thread Jason Wong
On Tuesday 10 December 2002 09:45, Steve Keller wrote:
> At 12/9/2002 07:20 PM,  John W. Holmes wrote:
>  > It'd be easier to use a regular expression for something like this.
>  > Something like this would work (from PHP Architect):
>
> I appreciate that, but considering I'm working from about 1200 files that
> already exist and use [] to denote table names, I kinda gotta do it this
> way.

John's suggestion of using '{', and '}' as the tag delimiters was to simplify 
the regex. You can continue to use '[', and ']' as your delimiters, just 
change the regex accordingly -- and don't forget that '[', and ']' needs to 
be escaped.

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *

/*
If life gives you lemons, make lemonade.
*/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Fwd: RE: [PHP] Odd Strpos Behavior

2002-12-09 Thread Steve Keller
At 12/9/2002 07:20 PM,  John W. Holmes wrote:

> It'd be easier to use a regular expression for something like this.
> Something like this would work (from PHP Architect):

I appreciate that, but considering I'm working from about 1200 files that 
already exist and use [] to denote table names, I kinda gotta do it this way.
--
S. Keller
UI Engineer
The Health TV Channel, Inc.
(a non - profit organization)
3820 Lake Otis Pkwy.
Anchorage, AK 99508
907.770.6200 ext.220
907.336.6205 (fax)
Email: [EMAIL PROTECTED]
Web: www.healthtvchannel.org


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Odd Strpos Behavior

2002-12-09 Thread John W. Holmes
> 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/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php