Re: variable gets lost in function problem again...
| From: | DL Neil | Date: | Fri, 26 Oct 2001 11:41:33 +0000 |
| Subject: | Re: variable gets lost in function problem again... | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-10053@lists.php.net to get a copy of this message | ||
Thanks for the added data, Spunk. I guess I should have asked for a view of the screen/echo
conversation too...
I'm not strong on RegExps so took a look at the manual
(http://www.php.net/manual/en/function.ereg-replace.php),
which says:
-----
string ereg_replace (string pattern, string replacement, string string)
This function scans string for matches to pattern, then replaces the matched text with replacement.
The modified string is returned. (Which may mean that the original string is returned if there are
no matches to
be replaced.)
-----
It seems to answer your question about "strings cf arrays". A string can be a character or
an array of
characters. An array can contain a list of strings - and we might thus call it a "string
array" but that does
not make it A "string".
So it seems to me [medium-high ignorance factor present] that you would definitely need to loop the
array
through the RegExp an element at a time; and that appears to be exactly what you're doing, but
once again,
without echo-ing so that you/we can see the data fore-and-aft, it's difficult to spot a problem
within the
crucial function call (lacking those RegExp skills as I do).
NB in talking about 'pass by reference' I was refering to the function you had written,
NOT the call to
ereg_replace()!
However [he continuous ponderously] again dipping into the manual
(http://www.php.net/manual/en/function.str-replace.php), we see:
-----
mixed str_replace (mixed search, mixed replace, mixed subject)
This function returns a string or an array with all occurences of search in subject replaced with
the given
replace value. If you don't need fancy replacing rules, you should always use this function
instead of
ereg_replace() or preg_replace().
In PHP 4.0.5 and later, every parameter to str_replace() can be an array.
If subject is an array, then the search and replace is performed with every entry of subject, and
the return
value is an array as well.
If search and replace are arrays, then str_replace() takes a value from each array and uses them to
do search
and replace on subject. If replace has fewer values than search, then an empty string is used for
the rest of
replacement values. If search is an array and replace is a string; then this replacement string is
used for
every value of search. The converse would not make sense, though.
-----
Note the comment about replace cf RegExp - a frequent refrain on this list is the improved
efficiency for simple
search/replace operations.
I feel sure that the idea of an array-search against an array-replace should come with a
Surgeon-General's
health warning to protect one's sanity, but this sort of challenge might appeal to you?
Unsought/pedantic advice: try putting a simple string (extracted from your data) through the chosen
transformation/function first, then replace the string with a string array element, then work up to
whole arrays
at a time... It seems so simplistic, but everyone should have a short 'testbench.php'
script into which code
fragments can be dropped, so that tests can be made on 'new' constructs. Then to use this
in a process of
gradual 'construction' (ie "make it work (simply) before you make it better"),
rather than starting from
something complex and having to dismantle/simplify it back down, in order to debug/check your logic
and
assumptions.
Hope it's helping,
=dn
PS I put this contribution back on the list in case it will help others/provoke a more learned
response
> Thanks for responding. Odly, my site is down at the moment so I will need
> some time to investigate. Here's what I know so far:
>
> I did debug with echo inside one of my functions and the string was fine
> until it used ereg_replace(). The same thing happens outside of my function
> (in the body of the script, calling ereg_replace($variable);). I tried
> passing the variable to my function as a reference but the same problem
> occurred. I didn't try to pass it as a reference to ereg_replace though...
> but I would guess ereg_replace accepts arguments as reference by default...
> Maybe this is the problem?
>
> I was planning on throwing all the code into one file to see if that worked
> but I have to wait till my server's back up.
>
> The problem seems to stem from passing a variable whose data is from an
> array that through ereg_replace ... should this be a problem? I define the
> variable with the array data in the body of my code... I can't see why this
> would be a problem but I'll check.
>
> Here's an example of my code:
>
> <?php
>
> include("getid3.php");
>
> function RemoveSpaces($stufftoreplace) //My function
> {
> // Removes spaces and other characters that could cause problems.
> $stufftoreplace = ereg_replace(" ", "_", "$stufftoreplace");
> return $stufftoreplace;
> }
>
> $id3info = (getID3("music.mp3"));
> $song = $id3info["TIT2"]["data"];
> $newsong = RemoveSpaces("$song");
> //$newsong = ereg_replace(" ", "_", "$song"); //This also shows
> problem!
> echo "$newsong"; //string is now empty
>
> Weird... Any thoughts?
>
> Spunk
>
>
> ?>
> >
> > Could you post some excerpted code please?
> >
> > You seem to have multiple issues:
> > 1 communication between a 'mainline' and INCLUDEd code
> > 2 communication between a 'mainline' and a function
> > 3 working with scalars and arrays
> > 4 working with text strings and ?text arrays
> > - which may or may not be related/relevant
> >
> > Let's start with simple ideas:
> > 0 put debug echo statements into the function (or everywhere) to track
> > progress
> > 1 if you take the processing code out of the INCLUDE file and put it in the
> > 'mainline', does it then work as planned?
> > 2 do you have an & to indicate a pass by reference cf a pass by value (see
> > manual)?
> > 3 and 4 does the processing use tools/approach/functions that only work with
> > scalars/arrays/strings?
> > A process of elimination...
> >
> > Keep us posted,
> > =dn
>
>