Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching - Regex

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA
Hi,

I'm trying to come up with a Regex that will change one specific url parameter.

Code:
$URL_STRING = $_SERVER['QUERY_STRING'];

$URL_STRING =~ s/lang=en/lang=ru/;

My Regex is pure perl, so I was wondering what is the equivalent in PHP ?

Thank you
 
My grasp of perl syntax is a bit rusty, but I think the functional equivalent is:

Code:
<?php
$URL_STRING = $_SERVER['QUERY_STRING'];

$URL_STRING = preg_replace('/lang=en/', 'lang=ru', $URL_STRING);
?>

The beginning "p" in "preg_replace" stands for "perl", so the regular expression syntax should be familiar to you.



Want the best answers? Ask the best questions! TANSTAAFL!
 

I'll take a look at the preg_replace function. Is there a simple pure PHP solution for this ?
 
simple pure PHP solution"?

preg_replace() is a builtin PHP function. It just happens to be an interface into a builtin copy of the perl regular expression engine. According to the intro to the PCRE (perl-compatible regular expression) section of the manual ( the PCRE functions require no external libraries and have been turned on by default since PHP version 4.2.0. And of the two regular expression systems supported by PHP, the PCRE family of functions are recommended by the online manual.

That's about as pure a PHP solution as you're going to get. And when you're talking regular expressions, there may not necessarily be a simple solution.


To be honest, I gave you the less-than-optimal solution. If you want to do a simple non-regular-expression string replacement (which your question is), then the PHP online manual recommends not using regular expressions. The better version of the code would be:

Code:
<?php
$URL_STRING = $_SERVER['QUERY_STRING'];

$URL_STRING = str_replace('lang=en', 'lang=ru', $URL_STRING);
?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top