ok so what i have going right now is a little system that takes a value of %address% and turns it into what is stored on the database. and i am using a str_replace

example (note there are more in the array)

$searchArray = array("%address%");
$replaceArray = array($address);
$content = str_replace($searchArray, $replaceArray, $content);

my problem is that i had oh maybe about 50 values that get replaced and when i added up to or more than i believe 100 the values dont get replaced.

now my question is what is more powerful str or preg_replace for a big project like this. or do you think it has to do with the array can only hold so much info.

thanks in advance

Recommended Answers

All 4 Replies

If I understand your question correctly you have an array of about 100 values, all of which may (or may not) contain the text string "%address%". This array is called $content. Then you want to replace the text "%address% wherever it may be found in $content, with the text in the variable $address. That is you are searching an array, replacing one value ("%address%") with one other value (the text found in $address). If this is the case, then the search and replace arguments should be simple variables, not arrays. Thus: $content = str_replace("%address%",$address,$content); (If I have misunderstood the question, perhaps you could clarify it?)

The search and replace arguments should only be arrays if they contain many different search/replace combinations, in which case it's like repeating the simpler form once for each element of the search array.

Which should you use, str_replace or preg_replace? Well, RTFM as they say!

From the PHP manual page for str_replace (http://be.php.net/manual/en/function.str-replace.php):

If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().

There is no limit (that I know of) to the size of an array, other than that of memory capacity of your computer, and possibly there may also be a limit set in you php.ini file, or somewhere else accessible to the system administrator. Anyway, if there is a limit, it's sure to be a lot more than a paltry 100 or so records!

i just checked my numbers and the first i had 54 and the amount i have now is 152

yes you are correct about what i mean. as stated i am replacing 152 values like address.
ex: "%address%", "%city%", "state" with $adderss, $city, $state

when i had it down to 54 first, it worked fine. but when i upped the number to 152 and seems that the %% values are not being replaced with the variables like before.

i thought that it could be that str_replace was not powerful enough for as many as i have.

if it may be a php.ini thing could you point me to a correct value that i have to change to test that theory out.

thanks

Nah, I really don't think that's it, I still think that you've got something wrong with your code. Did you try the line I gave you? (That would only work for %address% of course). Perhaps you could give a fuller sample of the code - although I don't mean pages and pages of it, just a bit more than 3 lines, showing how you are initialising the arrays etc.

However, if it is a resource limitation, then here's some settings from my php.ini file:

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
;max_input_nesting_level = 64 ; Maximum input variable nesting level
memory_limit = 128M      ; Maximum amount of memory a script may consume (16MB)

... but I think you would have to have much lower limits than that to have a program bomb out with just 100 odd array elements, unless they are very long. (160 records x 100 000 bytes each, 16 000 000 bytes) - you don't have records of a hundred thousand bytes. Do you???

If you read that preg_replace is "more powerful" than str_replace, then that is just that it has more powerful search abilities than a simple text replacement, not that it works any faster or has any greater capacity than str_replace.

crap. i think i found the problem. a syntax error that actually never showed up in a php error

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.