Hello ,

I'm trying to split a string using preg_split() ( since split ) is deprected...
I'm trying to split it using '=' sign and to get the GETS variable from a url but I'm getting the error :
"Warning: preg_split() [function.preg-split]: No ending delimiter '=' found in C:"

The code is :
<?php

$request_url = $_SERVER;

//parse the page request and get variables
$parsed_url = explode('&',$request_url );


$page_is = array_shift($parsed_url );

$getVars = array();
foreach($parsed_url as $argument)
{
//split GET vars along '=' symbol to separate variables and values
list($variable,$value) = preg_split("=",$argument);
$getVars[$variable] = $value;
}

//this is a test and we will be removing it later
print "the page you requested is '$page_is'";
print '<br>';
$vars = print_r($getVars,TRUE);
print "The following GET vars were passed to the page:<pre>".$vars."</pre>";

?>

Dani AI

Generated

A quick clarification and some practical alternatives.

The warning happened because preg_split() requires a regular‑expression pattern string complete with delimiters; supplying just = makes PHP treat = as the delimiter and then complain when it can't find the closing one. pointed that out and confirmed switching delimiters fixed the immediate error. For real use, prefer simpler, clearer parsing methods and make sure the code is reading the right server value (the original used $request_url = $_SERVER; which assigns the whole array—use $_SERVER['REQUEST_URI'] or $_SERVER['QUERY_STRING'] instead).

A robust approach: extract the query string and let PHP parse it into an array. This handles url-decoding and repeated keys correctly.

$req = $_SERVER['REQUEST_URI'];
$query = parse_url($req, PHP_URL_QUERY);
parse_str($query, $getVars); // $getVars is an associative array

If manual splitting is still desired, use explode() with a limit so values containing = stay intact, and rawurldecode() to decode percent-encoding:

$pairs = explode('&', $query);
foreach ($pairs as $pair) {
    $parts = explode('=', $pair, 2);
    $key = $parts[0];
    $value = isset($parts[1]) ? rawurldecode($parts[1]) : '';
    // store or validate $key/$value
}

Troubleshooting checklist: confirm which $_SERVER key is used, prefer $_GET or parse_str() for correctness, use explode() for simple splits (faster than preg_split()), and always validate/sanitize input (for example with filter_input_array() or filter_var()), especially before using values in SQL, filesystem, or output.

Recommended Answers

All 2 Replies

Weird way to get the GET params. Try this:

<?php
foreach ($_GET as $key => &$val) $val = filter_input(INPUT_GET, $key);

foreach ($_POST as $key => &$val) $val = filter_input(INPUT_POST, $key);
?>

or see this page: http://php.net/manual/fr/reserved.variables.get.php

Withing preg_split you should use a regular expression to split. An regular expression string starts with a character that also has to close the string. e.g. "!=!". In your case PHP thought the = was that character.

Hi. Thanks for the reply . I used "!=!" insted of "=" and didnt got the error.
Issue solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.