?
Solved

Storing expressions in variables for regex/pattern matching and replacing

Posted on 2005-03-17
15
Medium Priority
?
217 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:EddyGurge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +3
15 Comments
 
LVL 5

Expert Comment

by:jericotolentino
ID: 13571136
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. :-)
0
 
LVL 5

Expert Comment

by:jericotolentino
ID: 13571151
Whoops! That should have been <a href="linkTo.php?state=somestate">Some state</a>

It's also dynamically generated.
0
 
LVL 16

Expert Comment

by:vickerleung
ID: 13571188
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Author Comment

by:EddyGurge
ID: 13571190
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.
0
 
LVL 3

Author Comment

by:EddyGurge
ID: 13571259
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.
0
 
LVL 16

Expert Comment

by:vickerleung
ID: 13571412
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
0
 
LVL 3

Author Comment

by:EddyGurge
ID: 13571546
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 :(
0
 
LVL 5

Expert Comment

by:jericotolentino
ID: 13571986
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! :-)
0
 
LVL 11

Accepted Solution

by:
matt_mcswain earned 1000 total points
ID: 13572476
>>I don't think this query will be simplier than the nested if / switch case statement
Yeah, but it'd be alot cleaner to use the database if there's fifty of them.

And to EddyGurge's question, this is what eval() is for-->http://us2.php.net/eval
0
 
LVL 2

Expert Comment

by:Andy_Neodynium
ID: 13572881
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'.
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13573079
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.
0
 
LVL 3

Author Comment

by:EddyGurge
ID: 13574780
Thanks Matt, I knew there had to be a way!
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13575151
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.
0
 
LVL 2

Expert Comment

by:Andy_Neodynium
ID: 13575571
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;';
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13575851
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.
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question