DJCMBear 03-30-2010, 11:44 PM I have a string like "[section1][section2]" which i take the first and last charecters off so im left with "section1][section2"
But I cant seem to split it using this split code:
<?php
$str = "[section1][section2]";
$str = substr(substr($str,0,-1),1);
print_r(split('[\]\[]', $str));
?>
How could I do this? if anyone could help Thanks in advance.
Fou-Lu 03-30-2010, 11:47 PM Don't bother, use the trim command instead:
$result = trim($str, '][');
Optionally, you can just remove the first and last characters.
xconspirisist 03-30-2010, 11:50 PM This should do it:
$str = "[section1][section2]";
$str{0} = $str{strlen($str) - 1} = null;
$str = trim($str);
edit: Fou-Lu's example is more readable. We both posted at the same time.
DJCMBear 03-30-2010, 11:55 PM both very good solutions thank you I have done this but is it a good idea or is there a better way?
<?php
$str = "[section1][section2]";
$str = substr(substr($str,0,-1),1);
print_r(split("\]\[", $str));
?>
xconspirisist 03-30-2010, 11:58 PM I suggest you use Fou-Lu's example, as it is more readable and therefore, more maintainable!
DJCMBear 03-31-2010, 12:00 AM would that put the string into an array such as array(0 => 'section1', 1 => 'section2');?
xconspirisist 03-31-2010, 12:06 AM Oh sorry! I think we both missed that you wanted them split in two at the end. Here:
$str = "[section1][section2]";
$str = trim($str, "][");
$str = explode("][", $str);
print_r($str);
DJCMBear 03-31-2010, 12:15 AM damn I know about explode just didn't think about using that lol
You wouldn't happen to know how I could do this would you?
I have a string like (first|1|N)||(first|2|N)&&(go|1|N)&&(new|!1|N) which is like a more advanced if statement to break it down this is how its used (query|value|type) where the type can equal N, L or NL which mean numeric, letters or numeric and letters what im trying to do is make it so all I have to do is this callif("(first|1|N)||(first|2|N)&&(go|1|N)&&(new|!1|N)"); and it will check the url to see if the php query equals ?first=1&go=1&new=2 or ?first=2&go=1&new=2. And because the value for new says !1 then it will equal true if new=2 or new=3 but if new=1 then it will equal false.
xconspirisist 03-31-2010, 12:27 AM You need regex (http://uk3.php.net/manual/en/reference.pcre.pattern.syntax.php)!
DJCMBear 03-31-2010, 12:36 AM ok and would i need to use any foreach functions because I was thinking about splitting it by the '&&' and then if it contains '||' then split that with-in the foreach and check both against the url array.. would that be a good idea along with regex querying?
MattF 03-31-2010, 12:49 AM If it's a GET, use parse_url().
DJCMBear 03-31-2010, 01:11 AM thats a good idea MattF but with a normal if statement if you did if($_GET['first']=="1" || $_GET['first']=="2" && $_GET['go']=="1" && $_GET['new']!="1"); it would equal true if the url query equals ?first=1 or ?first=2&go=1&new=2 but I want mine to only be true when all prams are met such as ?first=1&go=1&new=2 or ?first=2&go=1&new=2. I was asking how I could do that.
MattF 03-31-2010, 01:14 AM Separate your match code. For example:
if(($_GET['first']=="1" && $_GET['go']=="1") || ($_GET['first']=="2" && $_GET['go']=="2"))
Matches if first and go are both 1 or first and go are both 2 but not on any other combination. Adapt to suit.
DJCMBear 03-31-2010, 02:32 AM I have created the function callif does it look ok?
<?php
function callif($str){
$C = split('&&',$str);
foreach($C As $_V => $_C){
$check = strpos($_C,'||')?true:false;
if($check === true){
$new_query = split("[||]",$C[$_V]);
foreach($new_query As $_S => $_G){
$G = split(':',substr(substr($_G,1),0,-1));
if(strtolower($G[2])=="n"){
$GF = is_numeric($_GET[$G[0]])?true:false;
}elseif(strtolower($G[2])=="l"){
$GF = preg_match('/^[a-zA-Z]+$/', $_GET[$G[0]])?true:false;
}elseif(strtolower($G[2])=="nl" || strtolower($G[2])=="ln"){
$GF = preg_match('/^[a-zA-Z0-9]+$/', $_GET[$G[0]])?true:false;
}
if($G[0]!="" && $_GET[$G[0]]==$G[1] && $GF === true
|| $G[0]!="" && $G[1]=="all" && $GF === true){
$_FCall .= 1;
}elseif(substr($G[1],0,1)=="!" && $_GET[$G[0]]!=substr($G[1],1) && $GF === true){
$_FCall .= 1;
}
}
}else{
$G = split(':',substr(substr($_C,1),0,-1));
if(strtolower($G[2])=="n"){
$GF = is_numeric($_GET[$G[0]])?true:false;
}elseif(strtolower($G[2])=="l"){
$GF = preg_match('/^[a-zA-Z]+$/', $_GET[$G[0]])?true:false;
}elseif(strtolower($G[2])=="nl" || strtolower($G[2])=="ln"){
$GF = preg_match('/^[a-zA-Z0-9]+$/', $_GET[$G[0]])?true:false;
}
if($G[0]!="" && $_GET[$G[0]]==$G[1] && $GF === true
|| $G[0]!="" && $G[1]=="all" && $GF === true){
$_FCall .= 1;
}elseif(substr($G[1],0,1)=="!" && $_GET[$G[0]]!=substr($G[1],1) && $GF === true){
$_FCall .= 1;
}
}
}
$matches = strlen($_FCall);
$pattern = count($C);
if($matches==$pattern){
return 1;
}else{
return 0;
}
}
print callif("(first:1:N)||(first:2:N)||(second:2:N)&&(go:1:N)&&(new:all)");
?>
It seems to work 100% idk if anyone will spot anything wrong if you do please let me know, thank you.
|
|