/    Sign up×
Community /Pin to ProfileBookmark

Simple preg_match / regex question

I’m trying to match the contents of a foreach loop between
two hyphens to four numbers (no [A-Z]) and discard/unset
if these don’t match. Bonus points if you can consolidate my
code below into the preg_match/replace to keep these at 7
or under innumerations.

[code=php]$data = array( ‘geo_num’ => “‘0000’, ‘0000’, ‘0000’, ‘0000’, ‘0000’, ‘0000’, ‘0000’, ‘0000’, ‘0000’”);[/code]

[code=php]
foreach($data AS $k=>$v) {
if($k == ‘geo_num’) {

$tmp = $v;
if(sizeof(explode(‘,’, $tmp)) > 7) {

$tmp = explode(‘,’, $tmp);

foreach($tmp AS $k => $v) {

if($v != preg_match(‘/^[0-9]{4}/’)) {
// Don’t think the above works, validation?
}

$size = sizeof($tmp);

for($i=7; $i < $size; ++$i) { unset($tmp[$i]); }

$validated[$k] = implode(‘,’, $tmp);

} else { $validated[$k] = $v; }
}
[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 08.2010 — You've done a pretty good job of confusing me. ?

Could you maybe show a sample input and expected output?
Copy linkTweet thisAlerts:
@ehimeauthorDec 08.2010 — sure

[code=php]
$validate = array( 'geo_num' => "'0000', '0000', '0000', '0000', '0000', '0000', '0000'");
# Where the '0000' can really be '[0-9]'
# geo_num can only have a MAX of 7 clusters(strings?) of four digit numbers
[/code]



More Examples:
[code=php]
$validate = array( 'geo_num' => "'0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000'"); // invalid - 8 Clusters
$validate = array( 'geo_num' => "'0000', '0000', '0000', '0000', '0000', '0000', '0000'"); // valid - 7 Clusters
$validate = array( 'geo_num' => "'0000', '0000', '0000', '0000', '0000'"); // valid - 5 Clusters
$validate = array( 'geo_num' => ""); // invalid - No Clusters
[/code]


Here's three invalid Examples
[code=php]
$validate = array( 'geo_num' => "'00000', '0000', '0000', '0000', '0000', '0000', '0000'"); // invalid - $validate cluster 1 has five numbers, not four
$validate = array( 'geo_num' => "'000', '0000', '0000', '0000', '0000', '0000', '0000'"); // invalid - $validate cluster 1 has three numbers, not four
$validate = array( 'geo_num' => "'000a', '0000', '0000', '0000', '0000'"); // invalid - $validate cluster 1 contains alpha characters
[/code]
Copy linkTweet thisAlerts:
@NogDogDec 08.2010 — Something like this, maybe?
[code=php]
if(preg_match('/^[0-9]{4}(,s[0-9]{4}){0,6}$/', $string)) {
// valid
}
[/code]

I'm still not sure I get the "big picture", so possibly the above could be in a callback function to be used with array_filter(), perhaps?
Copy linkTweet thisAlerts:
@ehimeauthorDec 08.2010 — Only issue is that it will either return the entire string or no string,

but will not discard the portions of the string that are invalid. Maybe

foreach through or some such?
Copy linkTweet thisAlerts:
@ehimeauthorDec 08.2010 — Here Nog, this is what I'm trying to do, it works, but

is ugly and is resource intensive. There is a better way

I'm sure, I'm just not sure a better way to refine it.

input:
[code=php]
$data = array('geo_num' => "'100', '00a00', '2000', '3000', '4000', '5000', '6000', '7000', '8000', '9000', '1100', '1200', '1300'");
[/code]


code:
[code=php]
case 'geo_num' :

$v = preg_replace(array("/,/", "/'/"), '', $v);

$v = explode(' ', $v);
$size = sizeof($v);

$tmp = array();
foreach($v AS $int) {
if(is_numeric($int) && strlen($int) == 4) {
array_push($tmp, $int); // add back
}
}

for($i=7; $i < $size; ++$i) {
unset($tmp[$i]);
}

$validated[$k] = "'" . implode("', '", $tmp) . "'";

break;
[/code]


outputs:
[code=php]
Array ( [geo_num] => '2000', '3000', '4000', '5000', '6000', '7000', '8000' )
[/code]
Copy linkTweet thisAlerts:
@NogDogDec 08.2010 — I don't know if this is much better, but is probably what I would do:
[code=php]
$data = array('geo_num' => "'100', '00a00', '2000', '3000', '4000', '5000',
'6000', '7000', '8000', '9000', '1100', '1200', '1300'");

foreach($data as $key => &$value) {
if($key == 'geo_num') {
$parts = preg_split('/,s*/', $value);
$parts = array_slice(
array_filter(
$parts,
create_function(
'$val',
'return preg_match("/'[0-9]{4}'/", $val);'
)
),
0,
7
);
$value = implode(', ', $parts);
}
}

echo "<pre>".print_r($data,1)."</pre>";
[/code]
Copy linkTweet thisAlerts:
@ehimeauthorDec 08.2010 — That sir, is a ridiculously more elegant solution.

[code=php]$rep++;[/code]
Copy linkTweet thisAlerts:
@ehimeauthorDec 09.2010 — Here you can see the entire thing in action now, thanks for the create_function stuff, it was a awesome idea.

[code=php]

<?php

function paramListValidation($data) {

foreach($data AS $k=>&$v) {

switch($k) {
case 'geo_num' :
$parts = preg_split('/,s*/', $v);

$parts = array_slice(
array_unique(
array_filter(
$parts,
create_function(
'$val',
'return preg_match("/'[0-9]{4}'/", $val);'
)
)
),
0,
7);

$parts = implode(', ', $parts);

if(!$parts == 0 || !$parts == '') { $validated[$k] = $parts; } else { $validated[$k] = "'0000'"; }
break;

case 'gender' : $validated[$k] = ($v == "'M'" || $v == "'F'") ? $v : 'NULL'; break;

case 'lo_mo' : $validated[$k] = ($v < 11 && $v > 0 && is_int($v)) ? $v = str_pad($v, 2, "0", STR_PAD_LEFT) : 'NULL'; break;
case 'hi_mo' : $validated[$k] = ($v < 12 && $v > 2 && $data['lo_mo'] < $v && is_int($v)) ? $v = str_pad($v, 2, "0", STR_PAD_LEFT) : 'NULL'; break;

case 'lo_day' : $validated[$k] = ($v < 30 && $v > 0 && is_int($v)) ? $v = str_pad($v, 2, "0", STR_PAD_LEFT) : 'NULL'; break;
case 'hi_day' : $validated[$k] = ($v < 31 && $v > 1 && $data['lo_day'] < $v && is_int($v)) ? $v = str_pad($v, 2, "0", STR_PAD_LEFT) : 'NULL'; break;

case 'lo_age' : $validated[$k] = ($v < 15 && is_int($v)) ? $v : 'NULL'; break;
case 'hi_age' : $validated[$k] = ($v < 16 && $v > 0 && $data['lo_age'] < $v && is_int($v)) ? $v : 'NULL'; break;

case 'orphan_yn' : $validated[$k] = ($v == "'N'" || $v == "'Y'" || $v == "'N', 'Y'") ? $v : "'N', 'Y'"; break;
case 'hope_yn' : $validated[$k] = ($v == "'N'" || $v == "'Y'" || $v == "'N', 'Y'") ? $v : "'N', 'Y'"; break;
case 'handicap_yn' : $validated[$k] = ($v == "'N'" || $v == "'Y'" || $v == "'N', 'Y'") ? $v : "'N', 'Y'"; break;

case 'rsrv_camp_list' :
$parts = preg_split('/,s*/', $v);

$parts = array_slice(
array_unique(
array_filter(
$parts, create_function(
'$val',
'return preg_match("/[0-9]{6}/", $val);'
)
)
),
0,
10);

$parts = implode(', ', $parts);

if(!$parts == 0 || !$parts == '') { $validated[$k] = $parts; } else { $validated[$k] = 0; }
break;

case 'proj_in' : $validated[$k] = ($v == 'IN' || $v == 'NOT IN' ) ? $v : 'NOT IN'; break;
case 'proj_list' :
$parts = preg_split('/,s*/', $v);

$parts = array_slice(
array_unique(
array_filter(
$parts,
create_function(
'$val',
'return preg_match("/'[0-9]{7}'/", $val);'
)
)
),
0,
10);
$parts = implode(', ', $parts);

if(!$parts == 0 || !$parts == '') { $validated[$k] = $parts; } else { $validated[$k] = 0; }
break;

case 'rtn_cnt' : $validated[$k] = ($v < 76 && $v > 0 && is_int($v)) ? $v : 'NULL'; break;
case 'next_item_num' : $validated[$k] = 'NULL'; break; /** This query needs attention to get it included */

default : break;

}

}


$request = NULL;

$request .= "<query-replacement>";

$request .= "<geo_num>{$validated['geo_num']}</geo_num>";
$request .= "<gender>{$validated['gender']}</gender>";
$request .= "<lo_mo>{$validated['lo_mo']}</lo_mo>";
$request .= "<hi_mo>{$validated['hi_mo']}</hi_mo>";
$request .= "<lo_day>{$validated['lo_day']}</lo_day>";
$request .= "<hi_day>{$validated['hi_day']}</hi_day>";
$request .= "<lo_age>{$validated['lo_age']}</lo_age>";
$request .= "<hi_age>{$validated['hi_age']}</hi_age>";
$request .= "<orphan_yn>{$validated['orphan_yn']}</orphan_yn>";
$request .= "<hope_yn>{$validated['hope_yn']}</hope_yn>";
$request .= "<handicap_yn>{$validated['handicap_yn']}</handicap_yn>";
$request .= "<rsrv_camp_list>{$validated['rsrv_camp_list']}</rsrv_camp_list>";
$request .= "<proj_in>{$validated['proj_in']}</proj_in>";
$request .= "<proj_list>{$validated['proj_list']}</proj_list>";
$request .= "<rtn_cnt>{$validated['rtn_cnt']}</rtn_cnt>";

$request .= ($validated['type'] == 'ordered') ? "<next_item_num>{$validated['next_item_num']}</next_item_num>" : "";

$request .= "</query-replacement>";


$query = '<?xml version="1.0" encoding="UTF-8"?><standard-query-name>';
$query .= ($validated['type'] == 'ordered') ? 'READ:WEB_SVC_QUERY:LIST_OF_AVAILABLE_CHILDREN_ORDERED' : 'READ:WEB_SVC_QUERY:LIST_OF_AVAILABLE_CHILDREN_RANDOM';
$query .= '</standard-query-name>' . $request;
$query .= '<latency-tolerance>NONE</latency-tolerance></request></StandardQuery></soap:Body></soap:Envelope>';

return x2arr(worldvision_curlcall('http://ibudev.wvus.org/websvc/actions/wvsMessageRouter.php', $query, $errors, 0, $parts = NULL));;

}

$run = paramListValidation($data = array(

'type' => 'ordered',

'geo_num' => "'100', '00a00', '2000', '000000', '4000', '5000', '6000', '7000', '8000', '9000', '1100', '1200', '1300'",
'geo_num' => NULL,
'gender' => "'M'",

'lo_mo' => 3,
'hi_mo' => 4,

'lo_day' => 1,
'hi_day' => 19,

'lo_age' => 5,
'hi_age' => 4,

'orphan_yn' => "'Y'",
'hope_yn' => "'N'",
'handicap_yn' => "'Y', 'N'",

'rsrv_camp_list' => "0, 100, 00a00,
138891, 157941, 157941, 145564, 125996, 999999,
187455, 188923, 122727, 153469, 000555, 666666,
1200, 1300, 9000, 1100, 1200, 1300",

'proj_in' => 'IN',
'proj_list' => "'809881', '331784', '967590', '450161', '736383', '701693', '843994', '309426', '302532', '493283', '683483', '645059', '492706', '803427', '345434', '528933'",

'rtn_cnt' => 16,

'next_item_num' => NULL,
)
);

print_r($run);

?>
[/code]
×

Success!

Help @ehime spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...