Expressions régulières, chercher le contenu d'un autre site

Par Cruwp, il y a 15 ans


Bonjour, je cherche à prendre le contenu d'un autre site.
Voici mon code :

<?php
$text = file_get_contents('http://www.lesite.com/membre?mid=3107553');
$conv = array(
        '<br\/>Vérification de nom : <b>(.*?)(.*?)<\/b>' => '<strong>$2</strong>',
);
foreach($conv as $k=>$v){
        $k = '/'.$k.'/isU';
        $text = preg_replace($k,$v,$text);
        echo $text;
}
?>

Je ne vois pas seulement l'élément que j'ai sélectionné, mon regex est valide avec http://www.gethifi.com/tools/regex
Je vois l'affichage de la page $text...
Commment afficher seulement ma selection ?

11 réponses

Flohw, il y a 15 ans

c'est à cause des "?", situés juste avant la parenthese fermante, si mes souvenirs sont bons, cela ne la contabilise pas dans les variables "prédéfinies" comme $1, $2... donc ton $2 ne peut pas exister...

Cruwp, il y a 15 ans

Hm ça ne change rien, quand j'ai testé avec http://www.gethifi.com/tools/regex toutes mes variables étaient définies, et ça marchait.
Le problème ne vient pas de là.
En fait je vois toute la page html, je voudrais voir seulement $2.
J'ai essayé de remplacer <strong>$2</strong> par <h1>$2</h1>, mais la selection n’apparaît pas en <h1> :/

PhiSyX, il y a 15 ans

Hello...
Warning: preg_replace() [function.preg-replace]: Unknown modifier 'b' in... ?
Le soucis vient de la : </b> (Il faut ajouter un anti-slash) ^^

Cruwp, il y a 15 ans

Ah j'ai pas modifié mon post,
En fait, je veux afficher seulement ma selection avec regex, je veux pas avoir tout la page html (c'est ce que j'ai)
Je veux me retrouver avec <strong>$2</strong> sur ma page :)

palmer08, il y a 15 ans

En plus il a bien fait avec le br

PhiSyX, il y a 15 ans

Alors essaye comme çà :

<?php
$text = file_get_contents('http://www.lesite.com/membre?mid=3107553');
$conv = array(
    '<br\/>Vérification de nom : <b>(.*?)(.*?)<\/b>' => '<strong>$2</strong>'
);
$i = 0;
foreach ($conv as $k => $v) {
    if (preg_match('/' . $k . '/', $text, $matched)) {
        $matched = preg_replace('/' . $k . '/', $v, $matched);
        echo $matched$i], '<br />';
    }
    ++$i;
}
// print_r($conv);
?>
Cruwp, il y a 15 ans

Rien ne s'affiche, ma page est blanche.
:/
J'ai essayé de faire plusieurs print_r mais je pense que le if est la cause du probleme.

PhiSyX, il y a 15 ans

Chez moi ça marche bien... ^^

<?php
$content = file_get_contents('http://phisyx.localhost/flex/MVC/blog/friends');
$conv = array(
    '<img src="(http:\/\/(.*)\.localhost\/photo\.html\?updt=3)" alt="(Photo du blog)" \/>' => '<a href="$1">$2 : $3</a>',
    '<h2><a href="(.*)\/4.html" .*>(.*)<\/a><\/h2>' => '<h4><a href="$1/2.html">$2</a></h4>',
    '<div class="(clear)"><\/div>' => '<span style="color:red;">$1</span>'
);

foreach ($conv as $k => $v)
{
    // Marche sans if (mais mettre preg_match)
    if (preg_match('/' . $k . '/', $content, $matched) == 1) {
        $matched = preg_replace('/' . $k . '/', $v, $matched);
        // print_r($matched);
        echo $matched[0], '<br />', "\n";
    }
}
?>

Code source normal :

<img src="http://phisyx.localhost/photo.html?updt=3" alt="Photo du blog" /> 
<h2><a href="http://phisyx.localhost/4.html" onclick="window.open(this.href);return false;">Page 4</a></h2> 
<div class="clear"></div>

Code source après script executé :

<a href="http://phisyx.localhost/photo.html?updt=3">phisyx : Photo du blog</a><br /> 
<h4><a href="http://phisyx.localhost/2.html">Page 4</a></h4><br /> 
<span style="color:red;">clear</span><br />

Voilà... :]

Cruwp, il y a 15 ans

Je n'ai pourtant aucun résultat, je laisse le site où je souhaite prendre le pseudo pour vous aider.
Le code actuel :

<?php  
$content = file_get_contents('http://www.chapatiz.com/book/?mid=1');  
$conv = array(  
    '<br\/>Vérification de nom : <b>(.*?)(.*?)<\/b>' => '<h1>$2</h1>'
);  

foreach ($conv as $k => $v)  
{  
    // Marche sans if (mais mettre preg_match)  
    if (preg_match('/' . $k . '/', $content, $matched) 1) {  
        $matched = preg_replace('/' . $k . '/', $v, $matched);  
        // print_r($matched);  
        echo $matched[0], '<br />', "\n";  
    }  
}  
?>
PhiSyX, il y a 15 ans

Ha en effet, je vois... C'est à cause de l'accent à "Vérification"... Pas bien encodé.

$conv = array(
    '<br \/>V.rification de nom : <b>(.*)<\/b>' => '<h1>$1</h1>'
);

PS : Ajoute le U à la fin de la regex, il va trop loin sinon :p

Cruwp, il y a 15 ans

Parfait, merci bien !
Tout marche comme je voulais :)