PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > string matching position.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
string matching position.

Réponse
 
LinkBack Outils de la discussion
Vieux 26/03/2008, 17h22   #1
Bob Bedford
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut string matching position.

Hello,

I've a string and all sort of values in wich I've to check and return the
correct value.

Here is a sample (simplified as I've a huge amount of datas):

code = 1
subcode 65 values 06101;06102;06103;06106;06108;06109
subcode 69 values 07003;07004;07007;07008;07009;07012;07013

code = 2
subcode 65 values 61215;61218;61558;61587
subcode 69 values 83662:83667;83668

Now I've a given code and value and I need to know the subcode.

I mean if I give the function code=1 and value = 06108 then the function
should return 65
if I give the function code=2 and value = 83668 then the returned subcode
must be 69

How to do so ?

Thanks for ing.

Bob


  Réponse avec citation
Vieux 26/03/2008, 18h50   #2
petersprc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string matching position.

Hi,

You need a small routine to parse the data into an array.

This script will do it:

<?

function parseData($txt)
{
$lines = split("\n", $txt);
$codes = array();
$code = null;

for ($i = 0; $i < count($lines); $i++) {
$line = $lines[$i];
if (preg_match('/^code = (\d+)$/', $line, $m)) {
$code = $m[1];
} elseif (preg_match('/^subcode (\d+) values ((?:\d+*(?:\d+))
$/',
$line, $m)) {
if (is_null($code)) {
throw new Exception('No code set at line ' . ($i + 1) . '.');
}
$vals = split(';', $m[2]);
foreach ($vals as $val) {
$codes[$code][$val] = $m[1];
}
} elseif ($line == '') {
if (is_null($code)) {
throw new Exception('Unexpected empty line at line ' .
($i + 1) . '.');
}
$code = null;
} else {
throw new Exception('Unable to process line ' . ($i + 1) . '.');
}
}

return $codes;
}

function getSubcode($codes, $code, $val)
{
return isset($codes[$code]) && isset($codes[$code][$val]) ?
$codes[$code][$val] : false;
}

function test()
{
$txt = <<<end
code = 1
subcode 65 values 06101;06102;06103;06106;06108;06109
subcode 69 values 07003;07004;07007;07008;07009;07012;07013

code = 2
subcode 65 values 61215;61218;61558;61587
subcode 69 values 83662;83667;83668

end;

$codes = parseData($txt);

echo "Subcode for code 1 and value 06108 is " .
getSubcode($codes, 1, '06108') . ".<br>\n";

echo "Subcode for code 2 and value 83668 is " .
getSubcode($codes, 2, '83668') . ".<br>\n";

echo "Subcode for code 3 and value 12345 is " .
getSubcode($codes, 3, '12345') . ".<br>\n";
}

test();

?>

On Mar 26, 12:22 pm, "Bob Bedford" <b...@bedford.com> wrote:
> Hello,
>
> I've a string and all sort of values in wich I've to check and return the
> correct value.
>
> Here is a sample (simplified as I've a huge amount of datas):
>
> code = 1
> subcode 65 values 06101;06102;06103;06106;06108;06109
> subcode 69 values 07003;07004;07007;07008;07009;07012;07013
>
> code = 2
> subcode 65 values 61215;61218;61558;61587
> subcode 69 values 83662:83667;83668
>
> Now I've a given code and value and I need to know the subcode.
>
> I mean if I give the function code=1 and value = 06108 then the function
> should return 65
> if I give the function code=2 and value = 83668 then the returned subcode
> must be 69
>
> How to do so ?
>
> Thanks for ing.
>
> Bob


  Réponse avec citation
Vieux 26/03/2008, 20h42   #3
Bob Bedford
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string matching position.

Thanks a lot Peter, with your I've done it.

"petersprc" <petersprc@gmail.com> a écrit dans le message de news:
c3299cd2-9bca-483c-a6e3-ff841adc481f...oglegroups.com...
> Hi,
>
> You need a small routine to parse the data into an array.
>
> This script will do it:
>
> <?
>
> function parseData($txt)
> {
> $lines = split("\n", $txt);
> $codes = array();
> $code = null;
>
> for ($i = 0; $i < count($lines); $i++) {
> $line = $lines[$i];
> if (preg_match('/^code = (\d+)$/', $line, $m)) {
> $code = $m[1];
> } elseif (preg_match('/^subcode (\d+) values ((?:\d+*(?:\d+))
> $/',
> $line, $m)) {
> if (is_null($code)) {
> throw new Exception('No code set at line ' . ($i + 1) . '.');
> }
> $vals = split(';', $m[2]);
> foreach ($vals as $val) {
> $codes[$code][$val] = $m[1];
> }
> } elseif ($line == '') {
> if (is_null($code)) {
> throw new Exception('Unexpected empty line at line ' .
> ($i + 1) . '.');
> }
> $code = null;
> } else {
> throw new Exception('Unable to process line ' . ($i + 1) . '.');
> }
> }
>
> return $codes;
> }
>
> function getSubcode($codes, $code, $val)
> {
> return isset($codes[$code]) && isset($codes[$code][$val]) ?
> $codes[$code][$val] : false;
> }
>
> function test()
> {
> $txt = <<<end
> code = 1
> subcode 65 values 06101;06102;06103;06106;06108;06109
> subcode 69 values 07003;07004;07007;07008;07009;07012;07013
>
> code = 2
> subcode 65 values 61215;61218;61558;61587
> subcode 69 values 83662;83667;83668
>
> end;
>
> $codes = parseData($txt);
>
> echo "Subcode for code 1 and value 06108 is " .
> getSubcode($codes, 1, '06108') . ".<br>\n";
>
> echo "Subcode for code 2 and value 83668 is " .
> getSubcode($codes, 2, '83668') . ".<br>\n";
>
> echo "Subcode for code 3 and value 12345 is " .
> getSubcode($codes, 3, '12345') . ".<br>\n";
> }
>
> test();
>
> ?>
>
> On Mar 26, 12:22 pm, "Bob Bedford" <b...@bedford.com> wrote:
>> Hello,
>>
>> I've a string and all sort of values in wich I've to check and return the
>> correct value.
>>
>> Here is a sample (simplified as I've a huge amount of datas):
>>
>> code = 1
>> subcode 65 values 06101;06102;06103;06106;06108;06109
>> subcode 69 values 07003;07004;07007;07008;07009;07012;07013
>>
>> code = 2
>> subcode 65 values 61215;61218;61558;61587
>> subcode 69 values 83662:83667;83668
>>
>> Now I've a given code and value and I need to know the subcode.
>>
>> I mean if I give the function code=1 and value = 06108 then the function
>> should return 65
>> if I give the function code=2 and value = 83668 then the returned subcode
>> must be 69
>>
>> How to do so ?
>>
>> Thanks for ing.
>>
>> Bob

>
>



  Réponse avec citation
Vieux 26/03/2008, 22h45   #4
Alexey Kulentsov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string matching position.

quick&dirty solution

<?php

$data=<<<EOD
code = 1
subcode 65 values 06101;06102;06103;06106;06108;06109
subcode 69 values 07003;07004;07007;07008;07009;07012;07013

code = 2
subcode 65 values 61215;61218;61558;61587
subcode 69 values 83662;83667;83668

EOD;

// search function
function smart_search($data,$code,$value)
{
preg_match("/^code = {$code}[^=]+subcode (\\d+) values
[;\\d]*$value/m",$data,$res);
return isset($res[1]) ? $res[1] : false;
}


// test it
foreach(array('06101'=>1,'06103'=>1,'06109'=>1,'07 009'=>1,'07013'=>1
,'61215'=>2,'61587'=>2,'83668'=>2) as $value=>$code)
echo $value.' '.$code.' : '.smart_search($data,$code,$value)."\n";


?>

I think here was mistake in string 'subcode 69 values 83662:83667;83668'
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h19.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13415 seconds with 12 queries