Visualizzazione dei risultati da 1 a 9 su 9
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    225

    trovare un numero dentro una stringa

    Salve a tutti,

    sono davanti all' ennesimo problema , ogni volta sempre piu strani. Vorrei ,come da topic, riuscire a trovare dei numeri dentro ad una stringa e quindi trasformarli in collegamenti <a>.
    Ho una stringa tipo

    $stringa="12 oppure 13 o 15, anche 20 e 21";

    sto pensando di utilizzare explode ma non riesco a trovare un metodo.......

    avete una dritta ?

    GRAZIE A TUTTI
    SAT

  2. #2
    A meno che non vi sia un separatore, non serve a nulla utilizzare explode.

    è un chiaro problema di espressioni regolari.

    Leggi questo articolo e dovresti arrivarci

    http://php.html.it/articoli/leggi/85...ioni-regolari/

  3. #3
    Ciao.
    Prova questa
    Codice PHP:
    $stringa="12 oppure 13 o 15, anche 20 e 21";
    preg_match_all("/(\d+)/"$stringa,$match );
    var_dump($match); 
    in match hai tutti i numeri.

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  4. #4
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    225
    Originariamente inviato da whisher
    Ciao.
    Prova questa
    Codice PHP:
    $stringa="12 oppure 13 o 15, anche 20 e 21";
    preg_match_all("/(\d+)/"$stringa,$match );
    var_dump($match); 
    in match hai tutti i numeri.


    $stringa = "25+12 o 13 anche 21 e 31";
    preg_match_all("/(\d+)/", $stringa,$match );
    var_dump($match);

    ho questo risultato:
    array(2) { [0]=> array(0) { } [1]=> array(0) { } }

    credevo di avere tipo :
    array(2) { [0]=>25 , [1]=>12, [2]=>13 ...............


    GRAZIE CMQ
    SAT

  5. #5
    Originariamente inviato da satwanted
    $stringa = "25+12 o 13 anche 21 e 31";
    preg_match_all("/(\d+)/", $stringa,$match );
    var_dump($match);

    ho questo risultato:
    array(2) { [0]=> array(0) { } [1]=> array(0) { } }

    credevo di avere tipo :
    array(2) { [0]=>25 , [1]=>12, [2]=>13 ...............


    GRAZIE CMQ
    SAT
    Non mi risulta l'ho provato e il
    risultato è quello aspettato.

    PS.

    L'offset dell'array è sbagliato non può
    essere due è 0 o 1

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  6. #6
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    225
    io ho scritto :
    Codice PHP:
    $stringa="12 oppure 13 o 15, anche 20 e 21";
     
    preg_match_all("/(d+)/"$stringa,$match );
     
    var_dump($match); 
    ma il risultato mi dà:
    array(2) { [0]=> array(0) { } [1]=> array(0) { } }

    forse il tu hai PHP2

    LOL


    non capisco ......

  7. #7
    Codice PHP:
    $stringa "25+12 o 13 anche 21 e 31";
    preg_match_all("/(\d+)/"$stringa,$match );
    echo 
    "<pre>";
    print_r($match[0]); 
    risultato su php 5.2 win e su 5.2 linux


    Array
    (
    [0] => 25
    [1] => 12
    [2] => 13
    [3] => 21
    [4] => 31
    )
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  8. #8
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    225
    ciao whisher....

    mi vergogno a postare di nuovo , ma non riesco a capire xche se copio e incollo il codice da te scritto ricevo un risultato diverso dal tuo.

    Codice PHP:
    $stringa "25+12 o 13 anche 21 e 31";
    preg_match_all("/(d+)/"$stringa,$match );
    echo 
    "<pre>";
    print_r($match[0]); 
    Array
    (
    )
    anche facendo:
    Codice PHP:
    $stringa "25+12 o 13 anche 21 e 31";
    preg_match_all("/(d+)/"$stringa,$match );

    var_dump($match); 
    array(2) { [0]=> array(0) { } [1]=> array(0) { } }
    :master:

    GRAZIE
    SAT

  9. #9
    Originariamente inviato da satwanted
    ciao whisher....

    mi vergogno a postare di nuovo , ma non riesco a capire xche se copio e incollo il codice da te scritto ricevo un risultato diverso dal tuo.

    Codice PHP:
    $stringa "25+12 o 13 anche 21 e 31";
    preg_match_all("/(d+)/"$stringa,$match );
    echo 
    "<pre>";
    print_r($match[0]); 


    anche facendo:
    Codice PHP:
    $stringa "25+12 o 13 anche 21 e 31";
    preg_match_all("/(d+)/"$stringa,$match );

    var_dump($match); 


    :master:

    GRAZIE
    SAT
    Dimenticavo il BBcode il codice giusto è

    $stringa = "25+12 o 13 anche 21 e 31";
    preg_match_all("/(\d+)/", $stringa,$match );
    var_dump($match);

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.