IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Recherche de sous chaine dans un code HTML


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 16
    Par défaut Recherche de sous chaine dans un code HTML
    Salut à tous,

    J'aimerai faire de la recherche de sous chaine dans une chaine arbitraire comme du code HTML.
    C'est à dire j'ai un code source plus ou moins complexe :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <p>Ceci peut <span class="gras">&ecirc;</span>tre tro<em>p</em> bien.</p>
    Visuellement dans un navigateur on aurait quelque chose qui ressemble à ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Ceci peut être trop bien.
    Moi utilisateur je recherche dans le code source la sous chaine (ou mot) : être trop bien

    Attention le code source que je présente n'a pas forcément d'intérêt et peut être largement mieux écris pour faire l'affichage que je fais mais j'ai écris ainsi pour mettre en évidence la difficulté (ou non ça dépend pour qui...) que cette recherche représente.

    Pour donner une application concrete, il s'agit de faire du surlignement de texte dans une page HTML et cela de facon dynamique.

    C'est à dire l'utilisateur sélectionne un texte dans son navigateur et automatiquement le texte sélectionné sera encadré par un span ...
    Donc pour cela il faut être capable de retrouver la chaine sélectionnée, qui est du texte brute sans décoration html, dans le code HTML comme dans l'exemple cité.

    Peut etre que je prend le problème à l'envers ou par le mauvais bout.

    Si vous avez d'autres méthodes pour réaliser ce que je cherche moi je suis pour.

    Merci d'avance.

  2. #2
    Membre confirmé Avatar de floanne
    Inscrit en
    Février 2006
    Messages
    167
    Détails du profil
    Informations personnelles :
    Âge : 45

    Informations forums :
    Inscription : Février 2006
    Messages : 167
    Par défaut
    Bonjour.

    Je ferais ça comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    $ma_chaine = /* ta sélection */
     
    $ma_chaine_regex = "" ;
    /*
     *Pour tous les caractères 
     */
    foreach ($ma_chaine as $car)
    {
        $ma_chaine_regex = $ma_chaine_regex.$car."(?:\[\/?\w\])*" ;
    }
    $ma_chaine_regex = "#".$ma_chaine_regex."#i" ;
     
    preg_match_all ($ma_chaine_regex, $mon_text_html, $tab)
    petite explication de (?:\[\/?\w\])*
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    (:? : permet de ne pas capturé le contenu de la parenthèse 
    \[  : un croché ouvrant
    \/? : éventuellement un slash 
    \w : une lettre (ou un chiffre), tu peux remplacer par [A-Z] si tu veux
    \]  : un croché fermant
    )*  : fin de bloque qui peut ne pas être présent ou présent plusieur fois (le *)
    dans ton exemple : trop

    SI tu cherche "trop" tu va avoir la chaine
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $ma_chaine_regex = "#t(?:\[\/?\w\])*r(?:\[\/?\w\])*o(?:\[\/?\w\])*p(?:\[\/?\w\])*#i" ;
    qui matchera ton code html...

    En espérant avoir était clair...

    ++Florent.

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 16
    Par défaut
    Salut

    Tout d'abord merci pour ta réponse.

    Alors je t'avoue que mon niveau en Regex est assez faible.

    J'ai fais des tests avec ce que tu m'as donné, voici le code source de test :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
     
     
    //jeu de données
     
    $exemples[] = array('être trop', 'Ceci être peut être trop bien.');
    $exemples[] =  array('être trop', '<span>Ceci peut &ecirc;tre <br />trop </span>bien.');
    $exemples[] =  array('être trop', '<span>Ceci peut <a href="#">être trop</a> bien.</span>');
    $exemples[] =  array('être trop', '<span>Ceci peut <span>&ecirc;</span>t<span>r</span>e trop bien.</span>');
    $exemples[] =  array('etre trop', '<span>Ceci peut <span>e</span>t<span>r</span>e trop bien.</span>');
    $exemples[] =  array('trop', 'Ceci peut <span class="gras">&ecirc;</span>tre tro<em>p</em> bien.');
    $exemples[] =  array('trop', 'Ceci peut <span class="gras">e</span>tre tro<em>p</em> bien.');
    $exemples[] =  array('devices participating in a computer network, that uses', 'An <b>Internet Protocol</b> (<b>IP</b>) <b>address</b> is a numerical label that is assigned to devices participating in a <a href="http://en.wikipedia.org/wiki/Computer_network" title="Computer network" target="_blank">computer network</a>, that uses the <a href="http://en.wikipedia.org/wiki/Internet_Protocol" title="Internet Protocol" target="_blank">Internet Protocol</a> for communication between its nodes.<sup id="cite_ref-rfc760_0-0" class="reference"><a href="http://en.wikipedia.org/wiki/#cite_note-rfc760-0" target="_blank"><span>[</span>1<span>]</span></a></sup> An IP address serves two principal functions: host or network interface <a href="http://en.wikipedia.org/wiki/Identification_%28information%29" title="Identification (information)" target="_blank">identification</a> and location <a href="http://en.wikipedia.org/wiki/Logical_address" title="Logical address" target="_blank">addressing</a>. Its role has been characterized as follows: <i>"A <a href="http://en.wikipedia.org/wiki/Hostname" title="Hostname" target="_blank">name</a> indicates what we seek. An address indicates where it is. A route indicates how to get there."</i><sup id="cite_ref-rfc791_1-0" class="reference"><a href="http://en.wikipedia.org/wiki/#cite_note-rfc791-1" target="_blank"><span>[</span>2<span>]</span></a></sup>');
     
    foreach($exemples as $exemple) {
    	echo '====================== DEBUT ====================== <br />';
    	echo 'Recherche de : "'.$exemple[0].'" dans le code html "'.htmlentities(($exemple[1])).'"<br /><br />';
    	$str_len = strlen($exemple[0]);
    	$ma_chaine_regex = "" ;
    	for($i = 0; $i < $str_len; ++$i ) {
    		//$ma_chaine_regex = $ma_chaine_regex.$exemple[0][$i]."(?:\[\/?\w\])*" ;
    		$ma_chaine_regex = $ma_chaine_regex.'((<\w[^>]*>)*(<\/\w>)*)*'.$exemple[0][$i];//.'((<\w[^>]*>)*(<\/\w>)*)*' ;
    	}
     
    	$ma_chaine_regex = '#'.$ma_chaine_regex.'#i';
    	echo 'Regex : '.$ma_chaine_regex.'<br /><br />';
     
    	$html = convertHtmlToLatin1($exemple[1]);
    	$ret = preg_match_all($ma_chaine_regex, $html, $tab, PREG_OFFSET_CAPTURE );
    	//print_r($tab);
    	echo '<br /><br />';
    	if ( $ret ) {	
    		/*
    		print_r($tab[0][0]);
    		echo '<br /><br />';
    		*/
     
    		$start = substr($html,0, $tab[0][0][1]);
    		//$start = substr($html,0,-1);
    		$middle = $tab[0][0][0];//substr($html,$tab[0][0][1],strlen($tab[0][0][0]));
    		$end = substr($html,($tab[0][0][1]+strlen($tab[0][0][0])));
    		//htmlentities
    		echo htmlentities('Sortie 2 : '.$start.
    		'<span style="background-color:#ffff66">'.$middle.'</span>'.
    		$end).'<br />';
    	}
    	else {
    		echo ' -- Fonctionne pas -- <br /><br />';
    	}
    	echo '====================== FIN ====================== <br /><br />';
    }
    Pour les exemples simples ou on peut matcher la chaine recherchée dans le code html, c'est assez simple et ce que tu m'as donné fonctionne plutot bien.

    Voici le résultat résultant de l'exécution de ce code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
     
    ====================== DEBUT ======================
    Recherche de : "être trop" dans le code html "Ceci être peut être trop bien."
     
    Regex : #((<\w[^>]*>)*(<\/\w>)*)*Ã((<\w[^>]*>)*(<\/\w>)*)*ª((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*o((<\w[^>]*>)*(<\/\w>)*)*p#i
     
     
     
    Sortie 2 : Ceci être peut <span style="background-color:#ffff66">être trop</span> bien.
    ====================== FIN ======================
     
    ====================== DEBUT ======================
    Recherche de : "être trop" dans le code html "<span>Ceci peut &ecirc;tre <br />trop </span>bien."
     
    Regex : #((<\w[^>]*>)*(<\/\w>)*)*Ã((<\w[^>]*>)*(<\/\w>)*)*ª((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*o((<\w[^>]*>)*(<\/\w>)*)*p#i
     
     
     
    Sortie 2 : <span>Ceci peut <span style="background-color:#ffff66">être <br />trop</span> </span>bien.
    ====================== FIN ======================
     
    ====================== DEBUT ======================
    Recherche de : "être trop" dans le code html "<span>Ceci peut <a href="#">être trop</a> bien.</span>"
     
    Regex : #((<\w[^>]*>)*(<\/\w>)*)*Ã((<\w[^>]*>)*(<\/\w>)*)*ª((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*o((<\w[^>]*>)*(<\/\w>)*)*p#i
     
     
     
    Sortie 2 : <span>Ceci peut <span style="background-color:#ffff66"><a href="#">être trop</span></a> bien.</span>
    ====================== FIN ======================
     
    ====================== DEBUT ======================
    Recherche de : "être trop" dans le code html "<span>Ceci peut <span>&ecirc;</span>t<span>r</span>e trop bien.</span>"
     
    Regex : #((<\w[^>]*>)*(<\/\w>)*)*Ã((<\w[^>]*>)*(<\/\w>)*)*ª((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*o((<\w[^>]*>)*(<\/\w>)*)*p#i
     
     
     
    -- Fonctionne pas --
     
    ====================== FIN ======================
     
    ====================== DEBUT ======================
    Recherche de : "etre trop" dans le code html "<span>Ceci peut <span>e</span>t<span>r</span>e trop bien.</span>"
     
    Regex : #((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*o((<\w[^>]*>)*(<\/\w>)*)*p#i
     
     
     
    -- Fonctionne pas --
     
    ====================== FIN ======================
     
    ====================== DEBUT ======================
    Recherche de : "trop" dans le code html "Ceci peut <span class="gras">&ecirc;</span>tre tro<em>p</em> bien."
     
    Regex : #((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*o((<\w[^>]*>)*(<\/\w>)*)*p#i
     
     
     
    Sortie 2 : Ceci peut <span class="gras">ê</span>tre <span style="background-color:#ffff66">tro<em>p</span></em> bien.
    ====================== FIN ======================
     
    ====================== DEBUT ======================
    Recherche de : "trop" dans le code html "Ceci peut <span class="gras">e</span>tre tro<em>p</em> bien."
     
    Regex : #((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*o((<\w[^>]*>)*(<\/\w>)*)*p#i
     
     
     
    Sortie 2 : Ceci peut <span class="gras">e</span>tre <span style="background-color:#ffff66">tro<em>p</span></em> bien.
    ====================== FIN ======================
     
    ====================== DEBUT ======================
    Recherche de : "devices participating in a computer network, that uses" dans le code html "An <b>Internet Protocol</b> (<b>IP</b>) <b>address</b> is a numerical label that is assigned to devices participating in a <a href="http://en.wikipedia.org/wiki/Computer_network" title="Computer network" target="_blank">computer network</a>, that uses the <a href="http://en.wikipedia.org/wiki/Internet_Protocol" title="Internet Protocol" target="_blank">Internet Protocol</a> for communication between its nodes.<sup id="cite_ref-rfc760_0-0" class="reference"><a href="http://en.wikipedia.org/wiki/#cite_note-rfc760-0" target="_blank"><span>[</span>1<span>]</span></a></sup> An IP address serves two principal functions: host or network interface <a href="http://en.wikipedia.org/wiki/Identification_%28information%29" title="Identification (information)" target="_blank">identification</a> and location <a href="http://en.wikipedia.org/wiki/Logical_address" title="Logical address" target="_blank">addressing</a>. Its role has been characterized as follows: <i>"A <a href="http://en.wikipedia.org/wiki/Hostname" title="Hostname" target="_blank">name</a> indicates what we seek. An address indicates where it is. A route indicates how to get there."</i><sup id="cite_ref-rfc791_1-0" class="reference"><a href="http://en.wikipedia.org/wiki/#cite_note-rfc791-1" target="_blank"><span>[</span>2<span>]</span></a></sup>"
     
    Regex : #((<\w[^>]*>)*(<\/\w>)*)*d((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)*v((<\w[^>]*>)*(<\/\w>)*)*i((<\w[^>]*>)*(<\/\w>)*)*c((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)*s((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*p((<\w[^>]*>)*(<\/\w>)*)*a((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*i((<\w[^>]*>)*(<\/\w>)*)*c((<\w[^>]*>)*(<\/\w>)*)*i((<\w[^>]*>)*(<\/\w>)*)*p((<\w[^>]*>)*(<\/\w>)*)*a((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*i((<\w[^>]*>)*(<\/\w>)*)*n((<\w[^>]*>)*(<\/\w>)*)*g((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*i((<\w[^>]*>)*(<\/\w>)*)*n((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*a((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*c((<\w[^>]*>)*(<\/\w>)*)*o((<\w[^>]*>)*(<\/\w>)*)*m((<\w[^>]*>)*(<\/\w>)*)*p((<\w[^>]*>)*(<\/\w>)*)*u((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*n((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*w((<\w[^>]*>)*(<\/\w>)*)*o((<\w[^>]*>)*(<\/\w>)*)*r((<\w[^>]*>)*(<\/\w>)*)*k((<\w[^>]*>)*(<\/\w>)*)*,((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)*h((<\w[^>]*>)*(<\/\w>)*)*a((<\w[^>]*>)*(<\/\w>)*)*t((<\w[^>]*>)*(<\/\w>)*)* ((<\w[^>]*>)*(<\/\w>)*)*u((<\w[^>]*>)*(<\/\w>)*)*s((<\w[^>]*>)*(<\/\w>)*)*e((<\w[^>]*>)*(<\/\w>)*)*s#i
     
     
     
    Sortie 2 : An <b>Internet Protocol</b> (<b>IP</b>) <b>address</b> is a numerical label that is assigned to <span style="background-color:#ffff66">devices participating in a <a href="http://en.wikipedia.org/wiki/Computer_network" title="Computer network" target="_blank">computer network</a>, that uses</span> the <a href="http://en.wikipedia.org/wiki/Internet_Protocol" title="Internet Protocol" target="_blank">Internet Protocol</a> for communication between its nodes.<sup id="cite_ref-rfc760_0-0" class="reference"><a href="http://en.wikipedia.org/wiki/#cite_note-rfc760-0" target="_blank"><span>[</span>1<span>]</span></a></sup> An IP address serves two principal functions: host or network interface <a href="http://en.wikipedia.org/wiki/Identification_%28information%29" title="Identification (information)" target="_blank">identification</a> and location <a href="http://en.wikipedia.org/wiki/Logical_address" title="Logical address" target="_blank">addressing</a>. Its role has been characterized as follows: <i>"A <a href="http://en.wikipedia.org/wiki/Hostname" title="Hostname" target="_blank">name</a> indicates what we seek. An address indicates where it is. A route indicates how to get there."</i><sup id="cite_ref-rfc791_1-0" class="reference"><a href="http://en.wikipedia.org/wiki/#cite_note-rfc791-1" target="_blank"><span>[</span>2<span>]</span></a></sup>
    ====================== FIN ======================
    la fonction convertHtmlToLatin1() me permet de remplacer les code ascii du style "&ecirc;" par son équivalent "ê".
    Je sais qu'il y a sans doute une fonction php native pour faire ce genre de chose. Soit je ne suis pas tombé sur le bon ou soit il ne convient pas à mon application (html_entity_decode je crois)...

    Quoiqu'il en soit j'ai un peu modifié et adapté ton code mais il y a des cas pour lesquels ca ne fonctionne pas et j'ai un peu du mal à comprendre, peut etre que je n'ai pas assez de recul pour m'en rendre compte... Une aide serait la bienvenue.

    Merci d'avance.

  4. #4
    Membre confirmé Avatar de floanne
    Inscrit en
    Février 2006
    Messages
    167
    Détails du profil
    Informations personnelles :
    Âge : 45

    Informations forums :
    Inscription : Février 2006
    Messages : 167
    Par défaut
    Oups !

    Je me suis trompé, j'ai juste oublié qu'on pouvait mettre plusieurs lettre dans une balise.

    Il faut donc mettre :essaye et dit moi ce que ça donne.

    A plus, Florent.

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 16
    Par défaut
    Salut,

    Encore merci de te pencher sur mon problème.

    Malheureusement cela ne résoud pas mon probème. Ca match beaucoup moins bien par rapport a la version que j'ai soumise hier c'est à dire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
    function surligner3($html,$selection) {
     
    	$str_len = strlen($selection);
    	$ma_chaine_regex = "" ;
    	for($i = 0; $i < $str_len; ++$i ) {
    		$ma_chaine_regex = $ma_chaine_regex.'((<\w[^>]*>)*(<\/\w>)*)*'.
    											$selection[$i];
    	}
     
    	$ma_chaine_regex = '#'.$ma_chaine_regex.'#i';
     
    	$html_modifie = convertHtmlToLatin1($html);
     
    	$ret = preg_match_all($ma_chaine_regex, $html_modifie, 
    								$tab, PREG_OFFSET_CAPTURE );
    	if ( $ret ) {	
     
    		$start = substr($html_modifie,0, $tab[0][0][1]);
    		$middle = $tab[0][0][0];
    		$end = substr($html_modifie,($tab[0][0][1]+strlen($tab[0][0][0])));
    		return $start.'<span style="background-color:#ffff66">'.$middle.'</span>'.$end;
    	}
    	return null;	
    }
    Pour le résultat on a ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
     
    ====================== DEBUT ======================
    Recherche de : "être trop" dans le code html "Ceci être peut être trop bien."
     
    Sortie 3 : Ceci être peut <span style="background-color:#ffff66">être trop</span> bien.
    Sortie 4 : Ceci être peut <span style="background-color:#ffff66">être trop</span> bien.
    ====================== FIN ======================
    ====================== DEBUT ======================
    Recherche de : "être trop" dans le code html "<span>Ceci peut &ecirc;tre <br />trop </span>bien."
     
    Sortie 3 : <span>Ceci peut <span style="background-color:#ffff66">être <br />trop</span> </span>bien.
    Sortie 4 :
    ====================== FIN ======================
    ====================== DEBUT ======================
    Recherche de : "être trop" dans le code html "<span>Ceci peut <a href="#">être trop</a> bien.</span>"
     
    Sortie 3 : <span>Ceci peut <span style="background-color:#ffff66"><a href="#">être trop</span></a> bien.</span>
    Sortie 4 : <span>Ceci peut <a href="#"><span style="background-color:#ffff66">être trop</span></a> bien.</span>
    ====================== FIN ======================
    ====================== DEBUT ======================
    Recherche de : "être trop" dans le code html "<span>Ceci peut <span>&ecirc;</span>t<span>r</span>e trop bien.</span>"
     
    Sortie 3 :
    Sortie 4 :
    ====================== FIN ======================
    ====================== DEBUT ======================
    Recherche de : "etre trop" dans le code html "<span>Ceci peut <span>e</span>t<span>r</span>e trop bien.</span>"
     
    Sortie 3 :
    Sortie 4 :
    ====================== FIN ======================
    ====================== DEBUT ======================
    Recherche de : "trop" dans le code html "Ceci peut <span class="gras">&ecirc;</span>tre tro<em>p</em> bien."
     
    Sortie 3 : Ceci peut <span class="gras">ê</span>tre <span style="background-color:#ffff66">tro<em>p</span></em> bien.
    Sortie 4 :
    ====================== FIN ======================
    ====================== DEBUT ======================
    Recherche de : "trop" dans le code html "Ceci peut <span class="gras">e</span>tre tro<em>p</em> bien."
     
    Sortie 3 : Ceci peut <span class="gras">e</span>tre <span style="background-color:#ffff66">tro<em>p</span></em> bien.
    Sortie 4 :
    ====================== FIN ======================
    ====================== DEBUT ======================
    Recherche de : "devices participating in a computer network, that uses" dans le code html "An <b>Internet Protocol</b> (<b>IP</b>) <b>address</b> is a numerical label that is assigned to devices participating in a <a href="http://en.wikipedia.org/wiki/Computer_network" title="Computer network" target="_blank">computer network</a>, that uses the <a href="http://en.wikipedia.org/wiki/Internet_Protocol" title="Internet Protocol" target="_blank">Internet Protocol</a> for communication between its nodes.<sup id="cite_ref-rfc760_0-0" class="reference"><a href="http://en.wikipedia.org/wiki/#cite_note-rfc760-0" target="_blank"><span>[</span>1<span>]</span></a></sup> An IP address serves two principal functions: host or network interface <a href="http://en.wikipedia.org/wiki/Identification_%28information%29" title="Identification (information)" target="_blank">identification</a> and location <a href="http://en.wikipedia.org/wiki/Logical_address" title="Logical address" target="_blank">addressing</a>. Its role has been characterized as follows: <i>"A <a href="http://en.wikipedia.org/wiki/Hostname" title="Hostname" target="_blank">name</a> indicates what we seek. An address indicates where it is. A route indicates how to get there."</i><sup id="cite_ref-rfc791_1-0" class="reference"><a href="http://en.wikipedia.org/wiki/#cite_note-rfc791-1" target="_blank"><span>[</span>2<span>]</span></a></sup>"
     
    Sortie 3 : An <b>Internet Protocol</b> (<b>IP</b>) <b>address</b> is a numerical label that is assigned to <span style="background-color:#ffff66">devices participating in a <a href="http://en.wikipedia.org/wiki/Computer_network" title="Computer network" target="_blank">computer network</a>, that uses</span> the <a href="http://en.wikipedia.org/wiki/Internet_Protocol" title="Internet Protocol" target="_blank">Internet Protocol</a> for communication between its nodes.<sup id="cite_ref-rfc760_0-0" class="reference"><a href="http://en.wikipedia.org/wiki/#cite_note-rfc760-0" target="_blank"><span>[</span>1<span>]</span></a></sup> An IP address serves two principal functions: host or network interface <a href="http://en.wikipedia.org/wiki/Identification_%28information%29" title="Identification (information)" target="_blank">identification</a> and location <a href="http://en.wikipedia.org/wiki/Logical_address" title="Logical address" target="_blank">addressing</a>. Its role has been characterized as follows: <i>"A <a href="http://en.wikipedia.org/wiki/Hostname" title="Hostname" target="_blank">name</a> indicates what we seek. An address indicates where it is. A route indicates how to get there."</i><sup id="cite_ref-rfc791_1-0" class="reference"><a href="http://en.wikipedia.org/wiki/#cite_note-rfc791-1" target="_blank"><span>[</span>2<span>]</span></a></sup>
    Sortie 4 :
    ====================== FIN ======================
    Au cas ou j'aurai fais des bétises voici la version que tu m'as proposé :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
    function surligner4($html,$selection,$debut_recherche) {
     
    	$str_len = strlen($selection);
    	$ma_chaine_regex = "" ;
    	for($i = 0; $i < $str_len; ++$i ) {
    		//$ma_chaine_regex = $ma_chaine_regex.'((<\w[^>]*>)*(<\/\w>)*)*'.$selection[$i];
    		$ma_chaine_regex = $ma_chaine_regex."(?:[\/?\w+])*".$selection[$i];
    	}
     
    	$ma_chaine_regex = "#".$ma_chaine_regex."#i";
     
    	$html_modifie = convertHtmlToLatin1($html);
     
    	$ret = preg_match_all($ma_chaine_regex, $html_modifie, 
    								$tab, PREG_OFFSET_CAPTURE );
    	if ( $ret ) {	
     
    		$start = substr($html_modifie,0, $tab[0][0][1]);
    		$middle = $tab[0][0][0];
    		$end = substr($html_modifie,($tab[0][0][1]+strlen($tab[0][0][0])));
    		return $start.'<span style="background-color:#ffff66">'.$middle.'</span>'.$end;
    	}
    	return null;	
    }

  6. #6
    Membre confirmé Avatar de floanne
    Inscrit en
    Février 2006
    Messages
    167
    Détails du profil
    Informations personnelles :
    Âge : 45

    Informations forums :
    Inscription : Février 2006
    Messages : 167
    Par défaut
    Bon ok, en testant ça marche mieux... J'avait fumé lol (j'avais remplacer les < et > par [ et ] entre autre...

    Donc voila la regex qui fonctionne : Pour tester et te documenter je te conseil ce site
    en bas de la page tu trouvera un testeur en ligne de regex que j'ai utiliser.

    Et rien que pour toi voila le lien sur ton exemple (qui marche)

    Oui enfin bon, presque, je viens de tester le lien et comme la regex est longue il l'a tronqué, du coup il manque la fin et le #

    A plus, Florent.

Discussions similaires

  1. rechercher une sous chaine dans une chaine
    Par id.prog dans le forum Débuter avec Java
    Réponses: 3
    Dernier message: 25/01/2009, 17h59
  2. Rechercher une sous chaine dans un chaine
    Par franck06 dans le forum Access
    Réponses: 2
    Dernier message: 20/09/2006, 14h53
  3. Recherche une sous-chaine dans un champ ?
    Par nerick dans le forum Requêtes
    Réponses: 3
    Dernier message: 06/03/2006, 13h46
  4. Rechercher une sous chaine dans une chaine
    Par annedjomo dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 04/02/2005, 10h36
  5. Rechercher une sous chaine dans une chaine
    Par Oluha dans le forum ASP
    Réponses: 4
    Dernier message: 03/02/2005, 14h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo