This is a discussion on HELP :: PHP regular expression question within the Coding Help forums, part of the Webmaster Help category; I have to change the values of several variables passed as a string containing url parameters. But only the "to_" ...
| |||||||
| Register | Forum Rules | FAQ | Donate | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| I have to change the values of several variables passed as a string containing url parameters. But only the "to_" date variables! "from_yyyy=2006&from_mm=03&from_dd=08&to_yyyy=2007 &to_mm=01&to_dd=08" Is preg_match the best option? Can someone give me advice on best way to approach the PCRE expression? |
| |||||
| Looks easy with preg_replace and an array of strings of each date. But I'm a crappy programmer, I could be wrong.
__________________ My Blog |
| |||||
| I think this is the most technically correct way to do it: Code: <?php
$output = array();
parse_str( 'from_yyyy=2006&from_mm=03&from_dd=08&to_yyyy=2007 &to_mm=01&to_dd=08', $output );
if( isset($output['to_yyyy']) ){
//New Year
$output['to_yyyy'] = '';
}
if( isset($output['to_mm']) ){
//New Month
$output['to_mm'] = '';
}
if( isset($output['to_dd']) ){
//New Day
$output['to_dd'] = '';
}
$new_str = '';
$i=0;
foreach( $output as $k=>$v )
{
$new_str .= (($i>0)?('&'):('')) . $k . '=' . $v;
$i++;
}
echo $new_str;
?>
|
| ||||
| DavidDoran, you are a legend. Works a treat and no sight-nor-sign of those presky regular expressions. Yet again I dodge the neccessity to get my hands dirty with the PCRL. |
| Tags |
| expression, php, question, regular |
| Thread Tools | |
| Display Modes | |
|
|
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PHP woes : checking if value is numeric | paul | Coding Help | 9 | 26-02-2007 02:50 PM |
| Bandwidth question | JamesA | Webmaster Discussion | 4 | 28-01-2007 10:56 AM |
| Joomla question | fobby | Coding Help | 7 | 05-01-2007 11:02 AM |
| PHP on windows | louie | Coding Help | 20 | 22-02-2006 06:25 PM |
| ||||||||
| | ![]() | |||||||