Ricercare uno specifico carattere in una precisa posizione

  • Creatore Discussione Creatore Discussione MoPa
  • Data di inizio Data di inizio

MoPa

Nuovo Utente
28 Mar 2016
16
0
1
Buondì,
ho un piccolo problema che non riesco a definire come fare.
Premetto che sono un "novizio" di PHP quindi potrei peccare anche nella spiegazione che cercherò di fare:

In pratica da un form action vengono trasportati dei valori e nella pagina che riceve questi valori ci sono delle verifiche.
In uno di questi campi obbligatoriamente vanno scritti 11 caratteri e alla settima posizione, sempre obbligatoriamente, deve essere presente il carattere - (trattino).
Esempio del campo da inserire aabbcc-1234

Nella pagina di controllo come posso verificare se il trattino è presente e che questo si trovi esattamente in settima posizione in modo da fare un if che se il carattere è assente o non nella corretta posizione dia l'errore, mentre se presente e nella corretta posizione, passi alla successiva verifica.

Grazie in anticipo
 
tratto dal manuale PHP con qualche variante, S.E.& O.
PHP:
<?php
$mystring = 'aabbcc-1234';
$findme   = '-';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of '-' was the 0th (first) character.
if ($pos === false) 
{
    echo "The string '$findme' was not found in the string '$mystring'";
} 
else 
{
    if ($pos != 6) 
    {
        echo "The string '$findme' was not found in the expected position of the string '$mystring'";
    } 
    else 
    {
        echo "The string '$findme' was found in the string '$mystring'";
        echo " and exists at position $pos";
    }
}
?>
 
Grazie Marino51 per la rapida risposta, lo script funziona, però, e mi scuso in anticipo se chiedo troppo, avrei necessità che mi facesse un altro controllo, che mi ero dimenticato a scriverlo prima.
In pratica oltre che alla effettiva presenza del carattere - e che questo sia inserito alla settima posizione, il codice dovrebbe verificare che il carattere - inserito nella variabile ce ne sia uno soltanto e non in un numero maggiore.
Grazie in anticipo.
 
potrebbe essere così,
PHP:
<?php 
$mystring = 'aabbcc-1234'; 
$findme   = '-'; 
$pos = strpos($mystring, $findme); 

// Note our use of ===.  Simply == would not work as expected 
// because the position of '-' was the 0th (first) character. 
if ($pos === false)  
{ 
    echo "The string '$findme' was not found in the string '$mystring'"; 
}  
else  
{ 
    if ($pos != 6)  
    { 
        echo "The string '$findme' was not found in the expected position of the string '$mystring'"; 
    }  
    else  
    { 
        if ( substr_count($mystring, $findme) > 1 )  
        { 
            echo "More than 1 occurrence of the string '$findme' in the string '$mystring'"; 
        }  
        else  
        { 
            echo "The string '$findme' was found in the string '$mystring'"; 
            echo " and exists at position $pos"; 
        }  
    } 
} 
?>
 
Grazie Infinite, senza il tuo prezioso aiuto non ci sarei arrivato.
Buona Pasquetta
 

Discussioni simili