0

別の Web サイトから解析された特定の単語を変更するには?

この PHP コードを使用して、サード パーティの Web サイトからテキストを解析しました。

<?php
 include_once 'externalcode/simple_html_dom.php';
set_time_limit(10);
/* update your path accordingly */
$url  ='http://maltadiocese.org/lang/en/parishes/attard/';
$html = file_get_html($url) or die ('Information about this Parish is currently unavailable');
foreach($html->find('span[lang=en]') as $webLink){
    echo $webLink->plaintext.'<br>';
    echo $webLink->href.'<br>';
} 
foreach($html->find('div[id=textwidget]') as $Link2){
    echo $webLink2->plaintext.'<div style="display:none";>';    
}    
?>  

テキストの解析に成功しました。ここで、解析されたテキスト内の特定の単語のスタイルを変更したいと思います。つまり、パリッシュチャーチオブザサンプションと言って、私のウェブサイトではプレーンテキストとして解析されます。太字にして赤く、下線を付けたいと思います。

このチュートリアルに従いましたが、うまくいきませんでした。

私の意図は、テキストを解析し、CSS ルールに従うように変更することです。

ありがとう

4

1 に答える 1

1

CSS でカスタム クラスを定義して、目的の効果を与えることができます (HTML クラスを呼び出しましょうfoo)。次に、コードで次のことを行います。

<?php
$textToFind = 'Parish Church of the Assumption';
$replace    = '<span class="foo">' . $textToFind . '</span>';
str_replace($textToFind, $replace, $parsedText);

次に、css スタイルシートで次のようにします。

.foo {
    text-decoration: underline;
    color: red;
    font-weight: bold;
}

編集:完全なコードのサンプル。

<html>
    <head>
        <style>
            .foo {
                text-decoration: underline;
                color: red;
                font-weight: bold;
            }
        </style>
    </head>
    <body>
    <?php
    $parsedText = fetch_text();
    $textToFind = 'Parish Church of the Assumption';
    $replace    = '<span class="foo">' . $textToFind . '</span>';
    echo str_replace($textToFind, $replace, $parsedText);
?>
    </body>
</html>
于 2013-02-16T01:04:30.503 に答える