I am working on my mini url router in PHP. So far I've got regex for extracting variables in the url enclosed in {}(By looking at Symfony's router code) :
public function parse($pattern)
{
$matches = '';
$variables = array();
$pos = 0;
preg_match_all('#\{\w+\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach ($matches as $match)
{
$varName = substr($match[0][0], 1, -1);
if(is_numeric($varName)) {
throw new Exception('Argument cannot be a number');
}
if (in_array($varName, $variables)) {
throw new \LogicException(sprintf('More then one occurrence of variable name "%s".', $varName));
}
$variables[] = $varName;
}
return ['variables' => $variables];
}
So this method extracts the vars, now I need a regex to match the pattern of the route with the url. I started learning about regexes, but I still am not that good to wrap my head around this.
This will work: \/hello\/{?([^\/}]+)}?
(At least it does on the examples you provided:)
/hello/{name}
/hello/Sam
Explanation
\/ is an escaped /
hello finds hello, in case you didn't realize.
{? finds a {, if there is one.
([^\/}]+) finds everything that's not a / or }
}? finds a }, if there is one.
For anyone that might be interested, I got it, thanks to Laurel's answer.
This is the updated version of the method it still needs some work though :
$matches = '';
$variables = array();
$pos = 0;
$reg = '#';
$nextText = '';
preg_match_all('#\{\w+\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach ($matches as $match)
{
$varName = substr($match[0][0], 1, -1);
$precedingText = substr($pattern, $pos, $match[0][1] - $pos);
$pos = $match[0][1] + strlen($match[0][0]);
$nxt = $pos - strlen($pattern);
if($nxt == 0) $nxt = strlen($pattern);
$nextText = substr($pattern, $nxt);
//$precedingText = substr($precedingText, 1, -1);
$precSegments = explode('/', $precedingText);
$precSegments = array_splice($precSegments, 1);
//$temp = 5;
echo 'nxt ' . $nextText . '<><br>';
if(strlen($precedingText) > 1)
{
foreach($precSegments as $key => $value)
{
$reg .= '\/';
$reg .= $value;
}
$reg .= '{?([^\/}]+)}?';
}
else
{
$reg .= '{?([^\/}]+)}?';
}
$nextText = str_replace('/', '\/', $nextText);
if(is_numeric($varName)) {
throw new Exception('Argument cannot be a number');
}
if (in_array($varName, $variables)) {
throw new \LogicException(sprintf('More then one occurrence of variable name "%s".', $varName));
}
$variables[] = $varName;
}
if(count($matches) < 1)
{
$reg .= str_replace('/', '\/', $pattern);
}
$reg = $reg . $nextText;
$reg .= '#';
if($pattern == '/')
{
$reg = '#^[\/]+$#';
}
return ['variables' => $variables, 'regex' => $reg];
Related
I'm trying to do some sort of translator which would be able to keep text uppercase/lowercase.
I need to replace it in PHP string and MySQL query too.
Example:
Potato is jumping all over the PLACE.
Potato is jumping all over the pLAcE. (optional)
Potato is jumping all over the place.
Potato is jumping all over the Place.
I want to replace word 'place' with 'garden'.
Potato is jumping all over the GARDEN.
Potato is jumping all over the gARdEe. (optional)
Potato is jumping all over the garden.
Potato is jumping all over the Garden.
It should also work with phrases.
I've created a function that will replace the word for you and keep the cases.
function replaceWithCase($source, $replacement, $string) {
// Does the string contain the source word?
if (strpos($string, $source) === false) {
return false;
}
// If everything is uppercase, return the replacement fully uppercase
if (ctype_upper($source)) {
return str_replace($source, strtoupper($replacement));
}
// Set an array to work with
$characters = array();
// Split the source into characters
$sourceParts = explode('', $source);
// Loop throug the characters and set the case
foreach ($sourceParts as $k => $sp) {
if (ctype_upper($sp)) {
$characters[$k] = true;
} else {
$characters[$k] = false;
}
}
// Split the replacement into characters
$replacementParts = explode('', $replacement);
// Loop through characters and compare their case type
foreach ($replacementParts as $k => $rp) {
if (array_key_exists($k, $characters) && $characters[$k] === true) {
$newWord[] = strtoupper($rp);
} else {
$newWord[] = strtolower($rp);
}
}
return substr_replace($source, implode('', $newWord), $string);
}
// usage
echo replaceWithCase('AppLes', 'bananas', 'Comparing AppLes to pears');
Note: it is untested and might need some tweaking
function stringReplace($findStr, $replaceStr, $str)
{
$isLowerStr = true;
for($i=0; $i<strlen($findStr); $i++){
if(ord($findStr[$i]) >= 97 && ord($findStr[$i])<=122){
if(ord($replaceStr[$i]) >= 65 && ord($replaceStr[$i])<=96){
$replaceStr[$i] = strtolower($replaceStr[$i]);
}else{
$replaceStr[$i] = $replaceStr[$i];
}
}else{
$isLowerStr = false;
$replaceStr[$i] = strtoupper($replaceStr[$i]);
}
}
if($isLowerStr == false){
if(strlen($replaceStr) > strlen($findStr)){
for($i=0;$i<(strlen($replaceStr)-strlen($findStr));$i++){
if(strtoupper($findStr) == $findStr){
$replaceStr[strlen($findStr)+$i] = strtoupper($replaceStr[strlen($findStr)+$i]);
}else{
$replaceStr[strlen($findStr)+$i] = strtolower($replaceStr[strlen($findStr)+$i]);
}
}
}
}
echo str_replace($findStr, $replaceStr, $str);die;
}
$findStr = 'Place';
$replaceStr = 'garden';
echo stringReplace($findStr, $replaceStr, 'Potato is jumping all over the '.$findStr.'.');
So I managed to create my own function in the end. Thanks for help and inspiration though.
function replaceWithCase($source, $replacement, $string, $pos = 0) {
while (($pos = strpos(strtolower($string), strtolower($source), $pos))!== false) {
$substr = mb_substr($string, $pos, strlen($source));
$remaining = mb_substr($string, $pos + strlen($source));
if (ctype_upper($substr)) {
$string = substr_replace($string,strtoupper($replacement),$pos,strlen($source));
continue;
}
$substrParts = preg_split('//u', $substr, null, PREG_SPLIT_NO_EMPTY);
$replaceParts = preg_split('//u', $replacement, null, PREG_SPLIT_NO_EMPTY);
$newWord = '';
foreach ($replaceParts as $k => $rp) {
if (array_key_exists($k,$substrParts))
$newWord .= ctype_upper($substrParts[$k]) ? mb_strtoupper($rp) : mb_strtolower($rp);
else
$newWord .= $rp;
}
$string = substr_replace($string,$newWord,$pos,strlen($source));
$pos = $pos + strlen($source);
}
return $string;
}
echo replaceWithCase("place", "garden", "Potato is jumping all over the PLACE");
echo "<br>";
echo replaceWithCase("jumping", "running", "Potato is jumping all over the pLAcE");
echo "<br>";
echo replaceWithCase("jumping", "cry", "Potato is jumping all over the place");
echo "<br>";
echo replaceWithCase("all", "", "Potato is jumping all over the Place");
echo "<br>";
echo replaceWithCase(" ", ";", "Potato is jumping all over the Place", 10);
echo "<br>";
Output:
Potato is jumping all over the GARDEN
Potato is running all over the pLAcE
Potato is cry all over the place
Potato is jumping over the Place
Potato is jumping;all;over;the;Place
Thanks #LadaB - that's a really helpful function but it also replaces partial word matches. Sometimes you might want this, but in others cases you won't, for example if you have:
$source = 'mom';
$replacement = 'mum'; // british spelling of "mom"
$string = 'Be in the moment with your Mom.';
You get: "Be in the mument with your Mum."
So, I have added an option to make the match "whole word only".
I found another issue where multibyte characters would shift where the capitalization ended up, which seems to be fixed by changing. mb_substr to substr.
I also removed the unused:
$remaining = substr($string, $pos + strlen($source));
My latest (I think fully working) version is:
function replaceWithCase($source, $replacement, $string, $wholeWordOnly = false) {
$pos = 0;
while (($pos = strpos(strtolower($string), strtolower($source), $pos))!== false) {
if($wholeWordOnly) {
preg_match("/\b".$string[$pos]."/", $string[($pos-1)] . $string[$pos], $start);
preg_match("/".$string[($pos+strlen($source)-1)]."\b/", $source . $string[($pos+strlen($source))], $end);
}
if(($wholeWordOnly && $start && $end) || !$wholeWordOnly) {
$substr = substr($string, $pos, strlen($source));
if (ctype_upper($substr)) {
$string = substr_replace($string,strtoupper($replacement),$pos,strlen($source));
continue;
}
$substrParts = preg_split('//u', $substr, -1, PREG_SPLIT_NO_EMPTY);
$replaceParts = preg_split('//u', $replacement, -1, PREG_SPLIT_NO_EMPTY);
$newWord = '';
foreach ($replaceParts as $k => $rp) {
if (array_key_exists($k,$substrParts))
$newWord .= ctype_upper($substrParts[$k]) ? mb_strtoupper($rp) : mb_strtolower($rp);
else
$newWord .= $rp;
}
$string = substr_replace($string,$newWord,$pos,strlen($source));
}
$pos = $pos + strlen($source);
}
return $string;
}
Hope that helps you or someone else.
Use "stripos" php function
$str = 'Potato is jumping all over the pLAcEs.';
$str_new = "garden";
$str1 = str_split($str_new);
$pos = stripos($str,'places');
$string = substr($str,$pos,6);
$extrct = str_split($string);
$var = '';
foreach ($extrct as $key => $value) {
if(ctype_upper($value))
{
$var .= strtoupper($str1[$key]);
}else{
$var .= strtolower($str1[$key]);
}
}
$new_string = str_replace($string, $var, $str);
echo $new_string; //Potato is jumping all over the gARdEn.
I'm trying to do some sort of translator which would be able to keep text uppercase/lowercase.
I need to replace it in PHP string and MySQL query too.
Example:
Potato is jumping all over the PLACE.
Potato is jumping all over the pLAcE. (optional)
Potato is jumping all over the place.
Potato is jumping all over the Place.
I want to replace word 'place' with 'garden'.
Potato is jumping all over the GARDEN.
Potato is jumping all over the gARdEe. (optional)
Potato is jumping all over the garden.
Potato is jumping all over the Garden.
It should also work with phrases.
I've created a function that will replace the word for you and keep the cases.
function replaceWithCase($source, $replacement, $string) {
// Does the string contain the source word?
if (strpos($string, $source) === false) {
return false;
}
// If everything is uppercase, return the replacement fully uppercase
if (ctype_upper($source)) {
return str_replace($source, strtoupper($replacement));
}
// Set an array to work with
$characters = array();
// Split the source into characters
$sourceParts = explode('', $source);
// Loop throug the characters and set the case
foreach ($sourceParts as $k => $sp) {
if (ctype_upper($sp)) {
$characters[$k] = true;
} else {
$characters[$k] = false;
}
}
// Split the replacement into characters
$replacementParts = explode('', $replacement);
// Loop through characters and compare their case type
foreach ($replacementParts as $k => $rp) {
if (array_key_exists($k, $characters) && $characters[$k] === true) {
$newWord[] = strtoupper($rp);
} else {
$newWord[] = strtolower($rp);
}
}
return substr_replace($source, implode('', $newWord), $string);
}
// usage
echo replaceWithCase('AppLes', 'bananas', 'Comparing AppLes to pears');
Note: it is untested and might need some tweaking
function stringReplace($findStr, $replaceStr, $str)
{
$isLowerStr = true;
for($i=0; $i<strlen($findStr); $i++){
if(ord($findStr[$i]) >= 97 && ord($findStr[$i])<=122){
if(ord($replaceStr[$i]) >= 65 && ord($replaceStr[$i])<=96){
$replaceStr[$i] = strtolower($replaceStr[$i]);
}else{
$replaceStr[$i] = $replaceStr[$i];
}
}else{
$isLowerStr = false;
$replaceStr[$i] = strtoupper($replaceStr[$i]);
}
}
if($isLowerStr == false){
if(strlen($replaceStr) > strlen($findStr)){
for($i=0;$i<(strlen($replaceStr)-strlen($findStr));$i++){
if(strtoupper($findStr) == $findStr){
$replaceStr[strlen($findStr)+$i] = strtoupper($replaceStr[strlen($findStr)+$i]);
}else{
$replaceStr[strlen($findStr)+$i] = strtolower($replaceStr[strlen($findStr)+$i]);
}
}
}
}
echo str_replace($findStr, $replaceStr, $str);die;
}
$findStr = 'Place';
$replaceStr = 'garden';
echo stringReplace($findStr, $replaceStr, 'Potato is jumping all over the '.$findStr.'.');
So I managed to create my own function in the end. Thanks for help and inspiration though.
function replaceWithCase($source, $replacement, $string, $pos = 0) {
while (($pos = strpos(strtolower($string), strtolower($source), $pos))!== false) {
$substr = mb_substr($string, $pos, strlen($source));
$remaining = mb_substr($string, $pos + strlen($source));
if (ctype_upper($substr)) {
$string = substr_replace($string,strtoupper($replacement),$pos,strlen($source));
continue;
}
$substrParts = preg_split('//u', $substr, null, PREG_SPLIT_NO_EMPTY);
$replaceParts = preg_split('//u', $replacement, null, PREG_SPLIT_NO_EMPTY);
$newWord = '';
foreach ($replaceParts as $k => $rp) {
if (array_key_exists($k,$substrParts))
$newWord .= ctype_upper($substrParts[$k]) ? mb_strtoupper($rp) : mb_strtolower($rp);
else
$newWord .= $rp;
}
$string = substr_replace($string,$newWord,$pos,strlen($source));
$pos = $pos + strlen($source);
}
return $string;
}
echo replaceWithCase("place", "garden", "Potato is jumping all over the PLACE");
echo "<br>";
echo replaceWithCase("jumping", "running", "Potato is jumping all over the pLAcE");
echo "<br>";
echo replaceWithCase("jumping", "cry", "Potato is jumping all over the place");
echo "<br>";
echo replaceWithCase("all", "", "Potato is jumping all over the Place");
echo "<br>";
echo replaceWithCase(" ", ";", "Potato is jumping all over the Place", 10);
echo "<br>";
Output:
Potato is jumping all over the GARDEN
Potato is running all over the pLAcE
Potato is cry all over the place
Potato is jumping over the Place
Potato is jumping;all;over;the;Place
Thanks #LadaB - that's a really helpful function but it also replaces partial word matches. Sometimes you might want this, but in others cases you won't, for example if you have:
$source = 'mom';
$replacement = 'mum'; // british spelling of "mom"
$string = 'Be in the moment with your Mom.';
You get: "Be in the mument with your Mum."
So, I have added an option to make the match "whole word only".
I found another issue where multibyte characters would shift where the capitalization ended up, which seems to be fixed by changing. mb_substr to substr.
I also removed the unused:
$remaining = substr($string, $pos + strlen($source));
My latest (I think fully working) version is:
function replaceWithCase($source, $replacement, $string, $wholeWordOnly = false) {
$pos = 0;
while (($pos = strpos(strtolower($string), strtolower($source), $pos))!== false) {
if($wholeWordOnly) {
preg_match("/\b".$string[$pos]."/", $string[($pos-1)] . $string[$pos], $start);
preg_match("/".$string[($pos+strlen($source)-1)]."\b/", $source . $string[($pos+strlen($source))], $end);
}
if(($wholeWordOnly && $start && $end) || !$wholeWordOnly) {
$substr = substr($string, $pos, strlen($source));
if (ctype_upper($substr)) {
$string = substr_replace($string,strtoupper($replacement),$pos,strlen($source));
continue;
}
$substrParts = preg_split('//u', $substr, -1, PREG_SPLIT_NO_EMPTY);
$replaceParts = preg_split('//u', $replacement, -1, PREG_SPLIT_NO_EMPTY);
$newWord = '';
foreach ($replaceParts as $k => $rp) {
if (array_key_exists($k,$substrParts))
$newWord .= ctype_upper($substrParts[$k]) ? mb_strtoupper($rp) : mb_strtolower($rp);
else
$newWord .= $rp;
}
$string = substr_replace($string,$newWord,$pos,strlen($source));
}
$pos = $pos + strlen($source);
}
return $string;
}
Hope that helps you or someone else.
Use "stripos" php function
$str = 'Potato is jumping all over the pLAcEs.';
$str_new = "garden";
$str1 = str_split($str_new);
$pos = stripos($str,'places');
$string = substr($str,$pos,6);
$extrct = str_split($string);
$var = '';
foreach ($extrct as $key => $value) {
if(ctype_upper($value))
{
$var .= strtoupper($str1[$key]);
}else{
$var .= strtolower($str1[$key]);
}
}
$new_string = str_replace($string, $var, $str);
echo $new_string; //Potato is jumping all over the gARdEn.
$url =file("list.txt");
foreach ($url as $sites) {
$sites = trim($sites);
echo $sites . " </ br>";
}
and list.txt contain some urls
http://example.com/cms/wp-content/themes/
http://example.com/wp-content/plugins/
http://example.com/wp-content/themes/Avada-Child-Theme/
how could i remove the word "/wp-content/" and everything after it
to be
http://example.com/cms
http://example.com
http://example.com
Take a look at the the parameter $before_needle at http://docs.php.net/strstr
$o = strstr($url, '/wp-content/', true);
How about using preg_replace?
Something like that:
$sites = trim(preg_replace( '#/wp-content.*#', '', $sites));
This should work:
<?php
$url =file("list.txt");
foreach ($url as $sites) {
$sites = trim($sites);
$pos = strpos($sites, 'wp-content');
$newStr = substr($sites,0,$pos );
echo $newStr . " </ br>";
}
?>
$lines = file('list.txt');
$find = '/wp-content/';
foreach ($lines as $line) {
$line = trim($line);
$pos = strpos($line, $find);
if($pos !== false) {
echo substr($line, 0, $pos) . '<br>';
} else {
echo 'Not found ' . $find . '<br>';
}
}
First explode your content by new line then loop through each and use substr function to remove the matches. Following function my be useful to you:
<?php
// can remove variables from: full url, from urls related to site root, form just a query string like "a=1&b=2"
if(!function_exists("remove_var_from_url")){
function remove_var_from_url($variable_name, $url_string){
// this is anything before the "?" sign
$base_url = '';
// the variable separator, can be "?" if is a full URL or can be empty, if we just have "&sort=sales&oprder=asc"
$separator = "";
$start_pos = 0;
$return_string = "";
//
if(strpos($url_string,"?")!==false){
$start_pos = strpos($url_string, "?")+1;
$separator = "?";
$base_url = substr($url_string, 0, $start_pos-1);
}
// start building the string from the base url (which can be empty)
$return_string = $base_url;
$url_vars_string = substr($url_string, $start_pos);
$names_and_values = explode("&", $url_vars_string);
//
foreach($names_and_values as $value){
list($var_name, $var_value) = explode("=", $value);
if($var_name != $variable_name){
// add the "?" once if needed
if(!$separator_added){
$return_string.= $separator;
$separator_added = true;
} else {
$return_string.= "&";
}
$return_string.= $var_name."=".$var_value;
}
}
// remove "&" from margins
$return_string = trim($return_string, "&");
// remove the "?" if is at the end, it means it was just one variable that was removed
$return_string = rtrim($return_string, "?");
return $return_string;
}
}
?>
I would rather suggest you to apply strpos on each of the string first. Strpos will return you the position of first occurance of a string. Then use substr to fetch everything prior to that string.
` $lines = file('list.txt');
$find = '/wp-content/';
foreach ($lines as $line) {
$position = strpos($line, '/wp-content');
if($position)
$string = substr($line, 0, $position);
}`
I have an array with many words, but some senteces are in uppercase. For example:
THIS SENTENCE IS UPPERCASE
this sentece is in lowercase
And I want to split this two sentences by \r\n, but I cant find how to do it
Here is how I retrive this array:
$result_pk_info = array();
while ($row = odbc_fetch_array($sql_pk_info))
{
$opisanie = iconv("cp1257", "UTF-8", trim($row['opis']));
$id = iconv("cp1257", "UTF-8", trim($row['pacientid']));
$date = iconv("cp1257", "UTF-8", trim($row['data']));
$desc = explode('##$', $opisanie);
$all_info = "<tr><td>".$desc[1]."</td></tr>";
$result_pk_info[] = $all_info;
}
in $desc I have words array in which I want to search and split uppercase and lowercase.
So can anyone help me with it?
UPD the text which I have hase something like this structure:
SENTENCE IN UPPERCASE Sentece in lower case
This function is what you're looking for :
function split_upper_lower ($string)
{
$words = explode (' ', $string);
$i = 0;
foreach ($words as $word)
{
if (ctype_upper ($word))
$new_string_array[++$i]['type'] = 'UPPER';
else
$new_string_array[++$i]['type'] = 'LOWER';
$new_string_array[$i]['word'] = $word;
}
$new_string = '';
foreach ($new_string_array as $key => $new_word)
{
if (!isset ($current_mode))
{
if (ctype_upper ($new_word))
$current_mode = 'UPPER';
else
$current_mode = 'LOWER';
}
if ($new_word['type'] === $current_mode)
{
$new_string .= $new_word['word'];
if (isset ($new_string_array[$key + 1]))
$new_string .= ' ';
}
else
{
$new_string .= "\r\n" . $new_word['word'];
if (isset ($new_string_array[$key + 1]))
$new_string .= ' ';
if ($current_mode === 'UPPER') $current_mode = 'LOWER';
else $current_mode = 'UPPER';
}
}
return $new_string;
}
Tested it with br :
$string = 'HI how ARE you doing ?';
echo split_upper_lower ($string);
Output :
HI
how
ARE
you doing ?
Use preg_match: http://php.net/manual/en/function.preg-match.php
$string = 'THIS SENTENCE IS UPPERCASE';
$string2 = 'this sentece is in lowercase';
if(preg_match ( '/[A-Z ]$/' , $string))
{
echo 'uppercase';
}
else
{
echo 'lowercase';
}
Use this in your loop through $desc. Where $string is one element of an array containing one string.
/[A-Z ]$/ will match all uppercase with spaces. You can just upgrade your regexp to grab something else from strings.
If I understood your question you can do like this ...
$desc = array ("abcd","ABCD","bbb","B");
foreach($desc as $value) {
if(ctype_upper($value)) {
// character is upper
} else {
// character is lower
}
}
This can be done using preg_match_all(). Use the following code,
$result_pk_info = array();
while ($row = odbc_fetch_array($sql_pk_info))
{
$opisanie = iconv("cp1257", "UTF-8", trim($row['opis']));
$id = iconv("cp1257", "UTF-8", trim($row['pacientid']));
$date = iconv("cp1257", "UTF-8", trim($row['data']));
$desc = explode('##$', $opisanie);
//converting $desc array to string
$string = implode(" " , $desc);
//Upper case Matching
$upprCase = preg_match_all('/[A-Z]/', $string, $uprmatches, PREG_OFFSET_CAPTURE);
if($upprCase){
foreach ($uprmatches as $match)
{
foreach($match as $value)
//Iam a uppercase
print $UpperCase = $value[0];
}}
//Splitting with \r\n
print "\r\n";
//lower case matching
$lowrCase = preg_match_all('/[a-z]/', $string, $lowrmatches, PREG_OFFSET_CAPTURE);
if($lowrCase){
foreach ($lowrmatches as $match)
{
foreach($match as $value)
//Iam a lowercase
print $lowerCase = $value[0];
}}
}
so I'm using this program http://code.google.com/p/ecartcommerce/source/browse/library/Ecart/Db/Table/Select/Disassemble.php?edit=1 to generate a string that corresponds to the zend db select command for creating that query..
but the thing screws up when I use an IN() statement in WHERE
so suppose I have
"SELECT * FROM j WHERE id IN (1,2,3,5,6)";
and I convert this using that class,
when I inspected the output, it stripped out the parentheses between the numbers in the IN clause, so it became
->where("id IN 1,2,3,4,5,6"), while it's supposed to be ->where("id IN (1,2,3,4,5,6)")
Anyone knows how to fix this?
I suspect it has something to do with the _addWhere method:
protected function _addWhere($where)
{
$result = '';
$where = 'AND ' . $where;
$replacement = true;
while ($replacement) {
$replacement = false;
$parts = preg_split(
'/(\(.+\)+)/U', $where, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
foreach ($parts as $part) {
if (!preg_match('/\(.+?\)/', $part)) {
continue;
}
if (strstr($part, ' AND ') || strstr($part, ' OR ')) {
continue;
}
$replacement = preg_replace('/^\s*\((.+)\)\s*$/', '$1', $part);
$where = str_replace($part, $replacement, $where);
}
}
$parts = preg_split(
'/(\(.*\))/U', $where, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
foreach ($parts as $part) {
if (preg_match('/^\(.*\)$/', $part)) {
$replacement = str_replace(
array('AND', 'OR'), array('AND--', 'OR--'), $part
);
$where = str_replace($part, $replacement, $where);
}
}
$parts = preg_split(
'/(AND|OR)\s/', $where, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
$type = 'where';
for ($i = 0 ; $i < count($parts) ; $i++) {
if ('OR' == $parts[$i]) {
$type = 'orWhere';
} else {
$type = 'where';
}
// $subQuery = str_replace('--', '', trim($parts[++$i],'() '));
$subQuery = preg_replace('/^\s*\((.+)\)\s*$/', '$1', $parts[++$i]);
$subQuery = str_replace('--', '', $subQuery);
$result .= "\r\t" . "->{$type}(\""
. $this->_replaceLongTableName(trim($subQuery)) . '")';
}
return $result;
}
It specifically screws up in the $replacement = preg_replace('/^\s*\((.+)\)\s*$/', '$1', $part); line...
To use IN parameter in where clause, try the following way
$i = array(1,2,3,4,5,6);
$select = $this->select()->from('table','*')->where('i = (?)',$i);
when the parameter for where is an array, Zend automatically splits by ","