Link to home
Start Free TrialLog in
Avatar of EddyGurge
EddyGurge

asked on

Storing expressions in variables for regex/pattern matching and replacing

I hope I can explain this properly.  I am creating a system that will generate a link for a user to sites from any state in the U.S.  The link is different on each site, but with a little massaging, it should be doable programatically.  The user will be prompted with a year and item number.  I would like to be able to store in a table, all the formulas necessary to deal with the 50 different ways that I have to build the link for it to work.  Is there a way to either store a regular expression, or actual php command(s) in a string, and then execute it(them)?  Sorry, this is way out of my realm as far as php goes.
Avatar of jericotolentino
jericotolentino
Flag of Philippines image

Hi EddyGurge,

I hope I understand your question correctly. The user goes to a page with a link to a site for their state?

You'll have to input the name of every individual state, unless there's a function for listing the states in the U.S.

So you'll have something like this:

<a href="linkTo.php?=state">Some state</a>

-------

Then your user will be redirected to the site for the state. On linkTo.php, they will be presented with a form that asks for the date and item number, then inputs the values into your database.

If you need help on input into the database, just post it. :-)
Whoops! That should have been <a href="linkTo.php?state=somestate">Some state</a>

It's also dynamically generated.
Avatar of Vicker Leung
EddyGurge

You can try this way

Say your main program is main.php

You can store your regular expression or php commands into other PHP files and make the main.php to include those PHP files
Say those files are expression_1.inc, expression_2.inc, expression_3.inc, etc.... (inc means the PHP include files)

So inside your main.php
It can be something like this

<?

     $include_name = "expression_1.inc"; // You can change this to expression_2 / expression_3 to run specific expressions

     include $include_name;

?>

In expression_1.inc

<?

     echo ("this is the expressions of expression_1");

?>

That's it
Vicker
Avatar of EddyGurge
EddyGurge

ASKER

Unfortunately, its a bit more complicated than that.  Say they put in a year and item number in a form.  I then need to generate a link to that state's page, in that state's link format.  It could be as simple as www.state.gov/?year=1234&item=7
Or, it could be as messy as www.state.gov/?z=asdf;oiar4984jtsldfgjhaplosij(year here)dffasldkf&kjasfdghoiaertgih4iun&alksdjfklasjdf&jnd(modiefied item number here)saguhn4gfk

By modified, it could be that that state changes item number 7 to 7-94 (7 is item number, 94 is legistlative session).  I know all the rules, but I don't want a giant, nested if or case statement to deal with all this.  I'd love to just save in a table, the formula to modiify the user's state/year/item input, and create the link to what they want.
vickerleung, although that would work, it would bascially be very close to the big case switch style statement.  I really really really want to save these expressions in a table on a SQL database.  The expressions could be as simple as replacing itemno and yearno with a number, or having to do some more complicated operations such as saying that item 7 for year 2005 would be 2005.7.94 (year 2005, item 7, leg year 94).  Basically I want to store formulas in a database that I can execute.  I hope this is making sense.
EddyGurge,

Even you store the expression inside a database table, you have to write queries to get the specify one
I don't think this query will be simplier than the nested if / switch case statement

Moreover, I don't think is possible to store some expressions into a string and make this expression to run

Vicker
That is what I was afraid of.  If I don't see anything else come up on this by tomorrow, I'll accept this answer sadly :(
EddyGurge,

I agree with vickerleung. Even if you can store the formulas in the database (which is possible) and manage to place it into the page (also possible), the page will already have been processed by PHP. Thus, you'll get your formula, not the result of it.

So better off with the long if or case statement. Good luck! :-)
ASKER CERTIFIED SOLUTION
Avatar of matt_mcswain
matt_mcswain

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yup, Eval. Although why bother having a database of them? I'd use a config file and include it....

$states['alaska']='$url = "http://www.alaska.gov/?=" . complicatedFunction( $item );'
$states['arizona'] = '$url = ......

....

Then in your code:

include 'states.php';

$state = 'alaska';

eval( $states[$state] );

Would set $url to your value for 'alaska'.
I don't see this being a problem at all. Although PHP will not evaluate strings within other strings, you can emulate it quite easily using simple substitution. For each state you need to store the preformatted string in the DB such as:

'www.state.gov/?year={ year }&item={ item }'

Then just do a search and replace on it (when it's stored in $string):

$url = str_replace(array('{ year }', '{ item }'), array($year, $item), $string);

I've deliberately used keys to search for that are invalid in URL encoding, so you're guaranteed not to find them in the actual URLs.

You could use eval, but you can't use it for simple string evaluation: this doesn't work:

$year = 2005;
$item = 23;
$a = '"?year={$year}&item={$item}"'; //String as if from the DB so skips PHP's string interpolation
print eval('$s = "$a"; print $s;');

I guess it would be useful to have a specific 'interpolate()' function that did the same as PHP's normal interpolation, but applied explicitly.
Thanks Matt, I knew there had to be a way!
Erm, but you've just moved the problem: complicatedFunction($item); will need to have a great big switch statement in it... the whole reason for having the URLs preformatted is that you can just drop the values in to the appropriate positions. Using a flat file is a good plan - we're only talking 50 lines of data after all.
Nope, it wouldn't - perhaps I should have been more explicit. You can have a different function for each line, so effectively you are pushing your 'if' into the indexing of the array. And you don't HAVE to use a function - as you say, for simpler URLs you might just drop the value in...

$states['alaska']='$url = "http://www.alaska.gov/?=" . alaskaFunction( $item );';
$states['arizona'] = '$url = "http://www.arizona.gov/?=" . arizonaFunction( $item );';
$states['alabama'] = '$url = "http://www.alabama.gov/?item=" . $item;';
So you're saying that writing up to 50 functions is simpler than a switch statement which was considered too big in the first place?!

I think that a better solution is to go with the simple array approach, but have 3 items for each state instead of just one: the URL, then a transform regex for each of item and year (which is what the guts of your custom functions would probably contain anyway). It may be that some sites require more than is possible with a regex - for example to do a lookup via some external site - in which case you could use functions for those few states (a 4th item in the array?), but it should be quite straightforward to do simple regex replacements using a single function for the majority of cases.