HELP :: PHP regular expression question

Status
Not open for further replies.

littleBird

New Member
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?
 

georgiecasey

New Member
Looks easy with preg_replace and an array of strings of each date. But I'm a crappy programmer, I could be wrong.
 

daviddoran

New Member
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;

?>
 

littleBird

New Member
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.
 
Status
Not open for further replies.
Top