Ciao a tutti, ho problemi con le regex per estrarre dati in php.
Ho trovato su internet alcuni modelli di regex e ho creato io quella per rilevare il numero di cellulare.
Da un testo, devo estrapolare tutte le informazioni: email, cellulare, telefono fisso, via/p.zza/c.da/..., url
$text = 'https://www.domain.com/ www.blah.com 3401234567 blablablabla +393401234567 blabla 340 1234567 , +39 3401234567 340-1234567 340/1234567 email@hotmail.it 340.1234567 +393351234567 maila@hotmail.it 123456789 00393401234567 08251234567 +3906448866323 164864865 http://prova.net/beta/index.php?event=111 http://www.prova.net/ http://prova.net/beta/ https://www.domain.com ';
Una delle poche che con preg_match_all dal sito http://www.phpliveregex.com/ mi restituisce qualcosa, è questa: (\+39)|(0039)?0\d+
Ma cosa sbaglio?
Grazie mille
(Mi scuso se il codice è sporco e pieno di commenti, purtroppo ho fatto una serie di tentativi e forse mi sono perso in essi)
codice:<?php function spulcia($regex, $text, $array){ // eregi($regex, $text, $array); // preg_match_all($regex, $text, $array, PREG_OFFSET_CAPTURE); preg_match_all($regex, $text, $array); return $array; } ?> -- ------------------------ <?php function stampa_arr($array){ /* è più bello questo di print_r, è solo per una stampa a video temporanea */ for($i=0; $i<=sizeof($array); $i++) echo("<li>".$array[$i]."</li>"); } $text = 'https://www.domain.com/ www.blah.com 3401234567 blablablabla +393401234567 blabla 340 1234567 , +39 3401234567 340-1234567 340/1234567 email@hotmail.it 340.1234567 +393351234567 maila@hotmail.it 123456789 00393401234567 08251234567 +3906448866323 164864865 http://prova.net/beta/index.php?event=111 http://www.prova.net/ http://prova.net/beta/ https://www.domain.com '; echo("Testo: <br><br>" . $text); $regex_email = '[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'; $regex_cell = '(\+39)|(0039)?3\d{2}\W?(\d{7}|\d{6})'; $regex_tel = '(\+39)|(0039)?0\d+'; $regex_url = '(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*'; echo("<br>" . "<br>"); $array_email = spulcia($regex_email, $text, $array_email); $array_cell = spulcia($regex_cell, $text, $array_cell); $array_tel = spulcia($regex_tel, $text, $array_tel); $array_url = spulcia($regex_url, $text, $array_url); $matches = $array_cell; print_r($array_cell); for ($i=0; $i< count($matches[0]); $i++) { echo "intero criterio: ".$matches[0][$i]."\n"; echo "parte 1: " . $matches[1][$i] . "\n"; echo "parte 2: " . $matches[3][$i] . "\n"; echo "parte 3: " . $matches[4][$i] . "\n\n"; } echo("poggibonsi"); preg_match_all($regex_tel, $text, $array); stampa_arr($array); /* echo("<br>E-Mail trovate: <br>"); //print_r($array_email); stampa_arr($array_email[0][0]); echo("<br>Cell trovati: <br>"); //print_r($array_cell); stampa_arr($array_cell); echo("<br>Tel trovati: <br>"); //print_r($array_tel); stampa_arr($array_tel); echo("<br>Url trovati: <br>"); //print_r($array_url); stampa_arr($array_url); */ /* preg_match_all($regex_email,$text,$matches_email); echo "<pre>" . print_r($matches_email,true) . "</pre>"; preg_match_all($regex_cell,$text,$matches_cell); echo "<pre>".print_r($matches_cell,true)."</pre>"; preg_match_all($regex_tel,$text,$matches_tel); echo "<pre>".print_r($matches_tel,true)."</pre>"; preg_match_all($regex_url,$text,$matches_url); echo "<pre>".print_r($matches_url,true)."</pre>"; */ ?>


Rispondi quotando

