Visualizzazione dei risultati da 1 a 9 su 9
  1. #1

    estrapolare variabili da stringhe

    buon inizio settimana (a me inizia il martedì).
    ho un problema, vorrei creare uno spider simulator ovvero uno script che presa una pagina web mi dica:

    1. cosa c'è nel metatag title
    2. cosa c'è nel meta tag description
    3. cosa c'è nel metatag keywords
    4. elaborare i dati

    il punto è come faccio a caricare in una variabile quello che trovo tra le virgolette del tag?

    esempio:

    <meta name="title" content ="titolo della pagina">

    vorrei dare alla variabile $title il valore "titolo della pagina" senza le virgolette

    come fare?
    Viviamo tutti in una palude di fango, ma alcuni di noi guardano le Stelle http://www.comunicareoltre.it

  2. #2
    Utente di HTML.it L'avatar di M4rko
    Registrato dal
    Dec 2000
    Messaggi
    619
    Te la devi giocare con le espressioni regolari, ma probabilmente cercando un po trovi qualcosa di già pronto, ad esempio:

    http://www.phpclasses.org/browse/package/1955.html
    http://www.phpclasses.org/browse/package/919.html
    Tutti hanno bisogno di credere in qualcosa.
    Io credo che mi farò un'altra birra.


  3. #3
    mi sono registrata e ho dato un'occhiata, mi sembra quello che può fare al caso mio, dato che non ho una grande esperienza con le classi, se ho problemi posso disturbarti ancora su questo tread? spero che la risposta sia si e ti ringrazio da ora
    Viviamo tutti in una palude di fango, ma alcuni di noi guardano le Stelle http://www.comunicareoltre.it

  4. #4
    Come avevo già detto non sono pratica nell' utilizzo delle classi, vediamo di imparate qualcosa:

    la classe raccoglie le funzioni che cercavo, tutte, quindi fa proprio al caso mio, c'è solo un problema, qundo provo a dargli in pasto la pagina html di prova che voglio esaminare non mi da tutte le informazioni promesse (ovviamente per un errore mio nell'implementazione dello script per lo sfruttamento della classe)

    provo a riportarlo per vedere se mi puoi aiutare o se qualcuno può farlo (riporto prima lo script e poi l'output che ottengo ed infine la classe, so che sono lunghi, ma c'è da soffermarsi solo sullo script):

    <?php
    include("html_info.class.php");

    $nomefile = $_POST["nomefile"];
    $fp = fopen ($nomefile, "r");
    $stringa = file_get_contents("$nomefile");
    fclose($fp);
    $info = new html_info($stringa);

    $titolo = $info->get_title();




    echo $info->get_title();
    echo ("
    \n");
    echo $info->get_meta_data();
    echo ("
    \n");
    echo $info->get_images();
    echo ("
    \n");
    echo $info->get_links();
    echo ("
    \n");
    echo $info->get_body();
    echo ("
    \n");
    echo $info->get_body_text();

    ?>
    l'output che ottengo è:

    pagina di prova
    Array
    Array
    Array



    pagina di prova testo di prova sull'intestazione Corpo del testo link1-link2
    La classe è:

    <?php
    /**
    * Class for getting general informations about html content
    * @author Sven Wagener <wagener_at_indot_dot_de>
    * @include Funktion:_include_
    */
    class html_info{

    var $string="";
    var $meta="";


    /**
    * Constructor of class html_info
    * @param string $html_string The whole HTML document as String
    * @desc Constructor of class html_info
    */
    function html_info($html_string){
    $this->string=$html_string;
    }

    /**
    * Returns the title
    * @return string $title the title of the HTML document
    * @desc Constructor of class html_info
    */
    function get_title(){
    $string=strtolower($this->string);
    preg_match_all("|<title>(.*)</title>|U",$string,$matches, PREG_PATTERN_ORDER);

    return $matches[1][0];
    }

    /**
    * Returns the meta data
    * @return array $matches the title of the HTML document
    * @desc Returns the meta data of the HTML document in an array ($matches[$i]['name'] and $matches[$i]['content'])
    */
    function get_meta_data(){
    $string=strtolower($this->string);
    preg_match_all("|<meta (.*)>|U",$string,$matches, PREG_PATTERN_ORDER);

    $k=0;
    $tmp_match_array="";

    // Putting all matches in an array
    for($i=0;$i<count($matches);$i++){
    for($j=0;$j<count($matches[$i]);$j++){
    if($matches[$i][$j]!=""){
    $tmp_match_array[$k]=$matches[$i][$j];
    $k++;
    }
    }
    }

    $matches="";

    // Getting detailed information of meta data and putting in array
    $k=0;
    for($i=0;$i<count($tmp_match_array);$i++){

    // Getting name
    preg_match_all("|name\=\"(.*)\" |U",$tmp_match_array[$i],$name_matches, PREG_PATTERN_ORDER);
    // Checking if entry not exists
    $found=false;
    for($j=0;$j<count($matches);$j++){
    if($matches[$j]['name']==$name_matches[1][0]){
    $found=true;
    }
    }
    if(!$found && $name_matches[1][0]!=""){
    $matches[$k]['name']=$name_matches[1][0];

    // Getting content
    preg_match_all("|content\=\"(.*)\"|U",$tmp_match_a rray[$i],$content_matches, PREG_PATTERN_ORDER);
    $matches[$k]['content']=$content_matches[1][0];
    $k++;
    }
    }

    $this->meta=$matches;
    return $matches;
    }

    /**
    * Returns all images
    * @return array $match the pictures and all information in an array
    * @desc Returns all images in an array ($match[$i]['src'], $match[$i]['alt'], $match[$i]['width'] and $match[$i]['height'])
    */
    function get_images(){
    $string=strtolower($this->string);
    preg_match_all("|<img (.*)>|U",$string,$matches, PREG_PATTERN_ORDER);

    // Putting all matches in an array
    for($i=0;$i<count($matches);$i++){
    for($j=0;$j<count($matches[$i]);$j++){
    if($matches[$i][$j]!=""){
    $tmp_match_array[$k]=$matches[$i][$j];
    $k++;
    }
    }
    }
    $k=0;
    for($i=0;$i<count($tmp_match_array);$i++){
    $found=false;
    for($j=0;$j<count($match);$j++){
    if($this->get_tag_param("src",$tmp_match_array[$i])==$match[$j]['src']){
    $found=true;
    }
    }
    if(!$found && $this->get_tag_param("src",$tmp_match_array[$i])!=""){
    $match[$k]['src']=$this->get_tag_param("src",$tmp_match_array[$i]);
    $match[$k]['alt']=$this->get_tag_param("alt",$tmp_match_array[$i]);
    $match[$k]['width']=$this->get_tag_param("width",$tmp_match_array[$i]);
    $match[$k]['height']=$this->get_tag_param("height",$tmp_match_array[$i]);
    $k++;
    }
    }

    return $match;
    }

    /**
    * Returns all links
    * @return array $match the links and all information in an array
    * @desc Returns all links in an array ($match[$i]['href'] and $match[$i]['target'])
    */
    function get_links(){
    $string=strtolower($this->string);
    preg_match_all("|<a (.*)>|U",$string,$matches, PREG_PATTERN_ORDER);

    // Putting all matches in an array
    for($i=0;$i<count($matches);$i++){
    for($j=0;$j<count($matches[$i]);$j++){
    if($matches[$i][$j]!=""){
    $tmp_match_array[$k]=$matches[$i][$j];
    // echo $tmp_match_array[$k]."
    \n";
    $k++;
    }
    }
    }

    $k=0;
    for($i=0;$i<count($tmp_match_array);$i++){
    $found=false;
    for($j=0;$j<count($match);$j++){
    if($this->get_tag_param("href",$tmp_match_array[$i])==$match[$j]['href']){
    $found=true;
    }
    }
    if(!$found && $this->get_tag_param("href",$tmp_match_array[$i])!=""){
    $match[$k]['href']=$this->get_tag_param("href",$tmp_match_array[$i]);
    $match[$k]['target']=$this->get_tag_param("target",$tmp_match_array[$i]);
    $k++;
    }
    }

    return $match;
    }

    /**
    * Returns all strings which are formated like the given parameter
    * @param boolean $bold if string have to be formatted bold choose true
    * @param boolean $italic if string have to be formatted italic choose true
    * @param boolean $underlined if string have to be formatted underlined choose true
    * @return array $strings the strings which have been found in an array
    * @desc Returns all strings in an array which are formated like the given parameter
    */
    function get_strings_formated($bold,$italic,$underlined){
    $i=0;
    if($bold){
    $tags[$i]['open']="";
    $tags[$i]['close']="
    ";
    $i++;
    }
    if($italic){
    $tags[$i]['open']="";
    $tags[$i]['close']="
    ";
    $i++;
    }
    if($underlined){
    $tags[$i]['open']="<u>";
    $tags[$i]['close']="</u>";
    $i++;
    }

    $strings=$this->get_strings_in_tags($tags,$this->string);

    return $strings;
    }

    /**
    * Returns all strings in $string which are given to the parameter $tags
    * @param array $tags the tags in an array ($tags[$i]['open'] and $tags[$i]['close'])
    * @param string $string the HTML string
    * @return array $strings the strings which have been found in an array
    * @desc Returns all strings in $string which are given to the parameter $tags
    */
    function get_strings_in_tags($tags,$string){
    for($i=0;$i<count($tags);$i++){
    $k=0;
    $pattern="|".$tags[$i]['open']."(.*)".$tags[$i]['close']."|U";
    preg_match_all($pattern,$string,$matches, PREG_PATTERN_ORDER);

    // Getting rest of all Tags
    for($j=0;$j<count($tags);$j++){
    if($tags[$j]['open']!=$tags[$i]['open'] && $tags[$j]['close']!=$tags[$i]['close']){
    $new_tags[$k]=$tags[$j];
    $k++;
    }
    }
    // Getting Strings from all matches
    for($j=0;$j<count($matches[1]);$j++){
    $new_string=$matches[1][$j];
    }

    if(count($tags)==1){
    for($j=0;$j<count($matches[1]);$j++){
    $end_matches[$j]=strip_tags($matches[1][$j]);
    }
    return $end_matches;
    }else{
    for($j=0;$j<count($matches[1]);$j++){
    $new_string=$matches[1][$j];
    $end_matches=array_merge($this->get_strings_in_tags($new_tags,$new_string),$end_m atches);
    }
    }
    }
    return $end_matches;
    }

    /**
    * Returns all strings in $string which are between the start and end tag
    * @param string $start_tag the starting tag
    * @param string $end_tag the end tag
    * @param string $string the string to search for
    * @return array $strings the strings which have been found pusched in an array
    * @desc Returns all strings in $string which are between the start and end tag
    */
    function get_strings_in_tag($start_tag,$end_tag,$string){
    $pattern="|".$start_tag."(.*)".$end_tag."|U";
    preg_match_all($pattern,$string,$matches, PREG_PATTERN_ORDER);
    for($j=0;$j<count($matches[1]);$j++){
    $array[$j]=$matches[1][$j];
    }
    return $array;
    }

    /**
    * Returns all strings which are headed (<h1> ... </h1> etc)
    * @param int $from_headnumber
    * @param int $till_headnumber
    * @return array $strings the strings which have been found pusched in an array
    * @desc Returns all strings which are headed (<h1> ... </h1> etc)
    */
    function get_strings_headed($from_headnumber,$till_headnumb er){
    $count_headers=$till_headnumber-$from_headnumber;
    $result_arr=array();

    for($i=$from_headnumber;$i<=$till_headnumber;$i++) {
    $results=$this->get_strings_in_tag("<h$i>","</h$i>",$this->string);
    if($results!=""){
    $result_arr=array_merge($result_arr,$results);
    }
    }
    return $result_arr;
    }

    /**
    * Returns the content of the body
    * @return string $bodytext The content of the body
    * @desc Returns the content of the body
    */
    function get_body(){
    // Getting body parametres
    $pattern="|<body(.*)>|U";
    preg_match_all($pattern,$string,$matches, PREG_PATTERN_ORDER);

    // Deleting body parameters
    $string=str_replace($matches[1][0],"",$string);
    echo "<xmp>".$string."</xmp>";
    $pattern="|<body>(.*)</body>|U";

    // Getting text in body
    $matches="";
    preg_match_all($pattern,$string,$matches, PREG_SET_ORDER);
    $string=$matches;

    for($i=0;$i<count($string);$i++){
    for($j=0;$j<count($string[$i]);$j++){
    echo "\$string[$i][$j]".$string[$i][$j]."
    ";
    }
    }
    }

    /**
    * Returns the content of the body without tags
    * @return string $bodytext the content of the body without tags
    * @desc Returns the content of the body without tags
    */
    function get_body_text(){
    $string=$this->string;

    $string=strip_tags($string);
    $string=str_replace("\n","",$string);
    $string=str_replace("\r","",$string);
    $string=str_replace("\t","",$string);
    $string=str_replace("<!--","",$string);
    $string=str_replace("//-->","",$string);
    $string=str_replace("","",$string);

    return $string;
    }

    /**
    * Returns the frame urls
    * @return array $frame_urls the urls of the frame in an array
    * @desc Returns the frame urls
    */
    function get_frame_urls(){
    }

    function get_tag_param($param,$tag){
    preg_match_all("|$param\=\"(.*)\"|U",$tag,$matches , PREG_PATTERN_ORDER);
    if($matches[1][0]==""){
    preg_match_all("|$param\=(.*)|U",$tag,$matches, PREG_PATTERN_ORDER);
    }
    if($matches[1][0]==""){
    preg_match_all("|$param\=\'(.*)\'|U",$tag,$matches , PREG_PATTERN_ORDER);
    }
    return $matches[1][0];
    }
    }
    ?>
    Il problema è che mi da quella risposta (array array array) che non riesco a gestire
    Viviamo tutti in una palude di fango, ma alcuni di noi guardano le Stelle http://www.comunicareoltre.it

  5. #5
    Utente di HTML.it L'avatar di M4rko
    Registrato dal
    Dec 2000
    Messaggi
    619
    Alcune funzioni ti restituiscono non una stringa, ma un array, ecco perchè non riesci a stamparne correttamente il risultato.

    Prova a fare ad esempio
    codice:
    echo '<pre>';
    print_r($info->get_meta_data());
    echo '</pre>';
    Per verificare il tipo di dato restituito da quella funzione (vedi www.php.net/print_r )
    Lo stesso vale per get_images() e get_links().

    Per stampare un array, invece di usare direttamente echo, dovrai fare un ciclo e stampare un elemento dell'array ad ogni iterazione.
    Tutti hanno bisogno di credere in qualcosa.
    Io credo che mi farò un'altra birra.


  6. #6
    Per visualizzare l'array che mi interessa uso il seguente ciclo:

    $meta = $info->get_meta_data();
    for($i=0;$i<count($meta);$i++){
    echo ("i meta della pagina sono:");
    echo ($meta[$i] . "
    ");
    }
    Non credo sia sbagliato, eppure il risultato è sempre quello:

    i meta della pagina sono: Array
    i meta della pagina sono: Array
    i meta della pagina sono: Array
    è come se mi restutisse il tipo anzichè il valore...io, invece voglio il valore perchè poi devo contare quanti caratteri ha ecc.

    Puoi Aiutarmi?
    Viviamo tutti in una palude di fango, ma alcuni di noi guardano le Stelle http://www.comunicareoltre.it

  7. #7
    Utente di HTML.it L'avatar di M4rko
    Registrato dal
    Dec 2000
    Messaggi
    619
    Ogni elemento dell'array $meta è a sua volta un array
    Per ogni array, hai un elemento "name" ed un elemento "content"

    codice:
    foreach ($meta as $k) {
        echo "Meta: {$k['name']} - Valore: {$k['content']}
    ";
    }
    Riprendendo invece il tuo ultimo codice, avresti dovuto usare, ad esempio, $meta[$i]['name'] e $meta[$i]['content'] dove fai l'echo

    Tutti hanno bisogno di credere in qualcosa.
    Io credo che mi farò un'altra birra.


  8. #8
    Stanotte alle 5 ho avuto l'illuminazione... si fa tutto con un ciclo for solo che io andavo a visualizzare direttamente l'array con echo e ho capito che non si può fare perchè l'output è esattamente "Array" quindi assegno ad una variabile il valore dell' elemento dell' array e visualizzo quello... funziona con un semplice for perchè il secondo indice è noto (ma php non permette cicli for nidificati come in altri linguaggi?è necessario usare foreach? questa cosa non mi è chiara, il mio manuale la tratta blandamente)

    Ora ho un altro problema, come faccio a contare quante volte una stringa si ripete in un'altra stringa?

    Con la funzione strstr trovo una stringa all' interno di un'altra una sola volta o no? apro un'altro tread?

    Grazie per l'aiuto sempre prezioso e utile.

    P.S. all' indirizzo che mi hai dato prima (quel sito che raccoglie le classi) ho trovato una classe per mandare sms (easysms class) mi sono registrata su easy sms e dal sito riesco a mandare ma con la classe faccio ben poco, mi puoi aiutare?

    TKS
    Viviamo tutti in una palude di fango, ma alcuni di noi guardano le Stelle http://www.comunicareoltre.it

  9. #9
    Utente di HTML.it L'avatar di M4rko
    Registrato dal
    Dec 2000
    Messaggi
    619
    [supersaibal]Originariamente inviato da carlaravelli
    Stanotte alle 5 ho avuto l'illuminazione... si fa tutto con un ciclo for solo che io andavo a visualizzare direttamente l'array con echo e ho capito che non si può fare perchè l'output è esattamente "Array" quindi assegno ad una variabile il valore dell' elemento dell' array e visualizzo quello... funziona con un semplice for perchè il secondo indice è noto (ma php non permette cicli for nidificati come in altri linguaggi?è necessario usare foreach? questa cosa non mi è chiara, il mio manuale la tratta blandamente)[/supersaibal]
    Puoi usare cicli nidificati tranquillamente. Ti ho portato un esempio con foreach semplicemente per abitudine (mia), non perchè ci sia una soluzione migliore dell'altra. Essendo abituato a foreach per scorrere gli array, lo trovo ora piu leggibile, ma è soltanto una scelta mia.


    Ora ho un altro problema, come faccio a contare quante volte una stringa si ripete in un'altra stringa?

    Con la funzione strstr trovo una stringa all' interno di un'altra una sola volta o no? apro un'altro tread?
    Direi substr_count()
    www.php.net/substr_count


    P.S. all' indirizzo che mi hai dato prima (quel sito che raccoglie le classi) ho trovato una classe per mandare sms (easysms class) mi sono registrata su easy sms e dal sito riesco a mandare ma con la classe faccio ben poco, mi puoi aiutare?
    Qua non so che dire, non ho mai usato quella classe. Ti conviene aprire un altro thread per avere aiuti specifici
    Tutti hanno bisogno di credere in qualcosa.
    Io credo che mi farò un'altra birra.


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.