NYCPHP Meetup

NYPHP.org

[nycphp-talk] dynamic string manipulation when web serving -, best practices and ideas?

inforequest sm11szw02 at sneakemail.com
Fri Jun 11 13:03:51 EDT 2004


Thanks for the suggestion. This approach is great for coding a 
particular page/template (php string functions are tremendous) but I 
suspect there is a more systematic solution that would allow such string 
replacements to be performed on all pages that match some criteria - 
systematic and site wide - perhaps even outside of PHP. The idea is that 
since only the rulesets and the database data will change, I don't want 
ot edit the page code. I suppose a set of INCLUDEs for the php 
processing could be used (Case ONE below) but is that the best way?

i.e. for Normal Page type = "X" (repeat for any other "type" of page 
which has a different strng processing ruleset):

Case ONE:
<mysql does SELECT>                         // never changes
<php filters and manipulate string content according to Ruleset for Page 
Type "X">   // <-- your str_replace + substr+whatever code
<php completes any other php work>    // never changes
<Apache deliver page>                           // never changes

CASE TWO:
<mysql does SELECT>                         // never changes
<php completes any other php work>     // never changes
<return control to Apache>                     // never changes
<Apache autoreplaces/strips/filters according to Ruleset for Page Type 
"X">  // <-- here lies the question. Can pass to external PHP scripts or 
something?
<Apache delivers page>                         // never changes

or maybe

CASE THREE:
<mysql does SELECT with a  manipulation according to Ruleset for Page 
Type "X">  // <-- here lies the question. Is MySQL a good choice for 
doing this, and if yes, how?
<php completes any other php work>     // never changes
<return control to Apache>                     // never changes
<Apache delivers page>                         // never changes

Are case 2 and 3 viable options? Aside from developer preference, is any 
one of these three offer significant benefits over the others? I 
consider maintaining the custom PHP code for each page type a draw back, 
as only the content and the filter/replace rulesets will be changing in 
this application (nothing else in the page code).

I hope that is clearer....




Jayesh Sheth jayeshsh-at-ceruleansky.com |nyphp 04/2004| wrote:

> Hello,
>
> I would say that the easiest way to do this would be to use (the 
> built-in PHP function) str_replace() after you have run the query.
>
> For example, suppose you have the following table:
>
> id | title | desc
> ---|-------|-----
> 1  | bob   | nickname
>
> Your goal is to replace "bob" with "Robert" when it appears in the 
> title field.
>
> So, a PHP function runs the following query:
> SELECT title FROM mytable WHERE id='1'
>
> [...]
>
> Then , you do something like:
> $title = str_replace("bob", "Robert", $title);
>
> Later, if you want "bob" to be replaced with "Roberta", you could just 
> do:
> $title = str_replace("bob", "Roberta", $title);
>
> I know my answer seems a bit simplistic; I hope I understood the 
> nature of your question correctly - perhaps their are more complex 
> replacement that you'd like to do.
>
> In any case, PHP has more than enough search and replace features - so 
> I would do the replacement from within PHP (and not in the SQL query 
> or elsewhere).
>
> Best Regards,
>
> - Jay
>
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
>




More information about the talk mailing list