Forum Moderators: DixonJones

Message Too Old, No Replies

Regular Expression Issue

Regular Expression

         

phpscripter

11:03 am on Apr 8, 2005 (gmt 0)

10+ Year Member



Hi there,
I need to filter particular substrings out of a string

<?php
$string = "search.php?go=search&Suchbegriff=&PLZ=&Stadt=&BundeslandDeutschland=Baden&BundeslandOesterreich=&KantonSchweiz=&Land=&Submit=Suchen";
?>

I only want the Strings(Substrings) between the '&'-chars each. Secondly i want to display them in some kind of order:

Suchbegriff=
PLZ=
Stadt=
BundeslandDeutschland=Baden
BundeslandOesterreich=
KantonSchweiz=
Land=
Submit=Suchen

Could somebody help me with the corresponding RegEx?
Thanks

phpscripter

11:32 am on Apr 8, 2005 (gmt 0)

10+ Year Member



got it on my own. Here is the code:

Code:

<?php

$string =
"/de/partner/search.php?go=search&Suchbegriff=&PLZ=&Stadt=&BundeslandDeutschland=Baden-Wuerttemberg&BundeslandOesterreich=&KantonSchweiz=&Land=&Submit=Suchen";
$muster = "/&+([^&])*/";

$res = array();

preg_match_all($muster,$string,$res);

print_r($res);

foreach($res[0] as $output){
echo $output."<br/>\n";

}

?>

Output:

&Suchbegriff=
&PLZ=
&Stadt=
&BundeslandDeutschland=Baden-Wuerttemberg
&BundeslandOesterreich=
&KantonSchweiz=
&Land=
&Submit=Suchen

coopster

12:55 pm on Apr 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, phpscripter.

You may also want to check into the PHP functions, parse_url() [php.net] and parse_str [php.net].

phpscripter

1:31 pm on Apr 11, 2005 (gmt 0)

10+ Year Member



Thank you.

frugalsnow

4:12 am on Apr 12, 2005 (gmt 0)

10+ Year Member



If you do more work with regular expressions, you may want to check out "The Regulator" - a free downloadable tool for windows that helps you build and test regexps.

[regex.osherove.com...]