2

私はこのかなり単純なサウンドの課題を設定しましたが、ドキュメントのdom要素にクラス名を挿入する方法を<body>理解しようとして立ち往生しています。

複雑なのは、関数を介して取得するHTMLマークアップを制御できないためですfile_get_contents(サードパーティがFTPを介してファイルをフィードします)。

したがって、body要素はさまざまな方法で構成できます。たとえば、次のようになります。

<body>
<body id="my-id" data-attribute="content">
<body data-attribute="content">
<body class="already-existing-class" id="my-id" data-attribute="content">

class=など…上記の属性の順序でさえ私の管理下にないので、前などがあるかもしれませんid=

私がここで話している複雑さを皆さんは理解していると思います。(私は願っています)

基本的に必要なのは、 (すでに存在する場合)の既存の属性に新しいクラスpreg_replace()挿入するか、新しいクラスを含む属性自体を追加するために使用する方法です。classbodyclass

どんな助けでも大歓迎です。

これがすでに回答されている場合は、遠慮なく指摘してください。検索してみましたが、そのような一般的な用語では、探しているものを見つけるのが困難でした。

読んでくれてありがとう。

J。

4

4 に答える 4

3

正規表現のみに近いソリューションを提供するために、余分なスペースが気にならない限り、これは機能します;-)

<?php

$pat = '/(<body) ?(([^>]*)class="([^"]*)")?/';
$inp = '<body>
<body id="my-id" data-attribute="content">
<body data-attribute="content">
<body class="already-existing-class" id="my-id" data-attribute="content">
<body id="my-id" data-attribute="content" class="abc">';

echo preg_replace($pat, '$1 $3 class="$4 new-class" ', $inp);

?>

出力のideoneを確認します。

于 2011-12-15T01:40:32.343 に答える
2

このアプリケーションでは、正規表現は非常に扱いにくい場合があります。代わりに、PHP の DOMDocument などの HTML パーサーを使用することをお勧めします。ここに例があります。

$node1 = '<body>';
$node2 = '<body id="my-id" data-attribute="content">';
$node3 = '<body data-attribute="content">';
$node4 = '<body class="already-existing-class" id="my-id" data-attribute="content">';

foreach( range( 1, 4) as $i)
{
    $var = 'node'.$i;
    $doc = new DOMDocument();
    $doc->loadHTML( $$var);
    foreach( $doc->getElementsByTagName( 'body') as $tag)
    {
        $tag->setAttribute('class', ($tag->hasAttribute('class') ? $tag->getAttribute('class') . ' ' : '') . 'some-new-class');
    }
    echo htmlentities( $doc->saveHTML()) . "\n";
}

デモ

<body>タグの出力が正しいことに注意してください。あなた (または別の SO メンバー) は、DOMDocument から body タグだけを抽出する方法を自由に決定できます。

于 2011-12-15T01:32:18.173 に答える
1
$str = '<body>
<body id="my-id" data-attribute="content">
<body data-attribute="content">
<body class="already-existing-class" id="my-id" data-attribute="content">
';

$my_new_class = "HELLO_WORLD";
preg_match_all("/<body(.*?)>/is", $str, $m);
$s = sizeof($m[1]);
for($i=0; $i<$s; $i++){
    $m[1][$i] = preg_replace("/class=\"(.*?)\"/is", "class=\"".$my_new_class."\"", $m[1][$i]);
    if(!preg_match("/class=/is", $m[1][$i])){
        $m[1][$i] .= " class=\"".$my_new_class."\"";
    }
    $m[1][$i] = "<body".$m[1][$i].">";
}

print_r($m);
[1] => Array
    (
        [0] => <body class="HELLO_WORLD">
        [1] => <body id="my-id" data-attribute="content" class="HELLO_WORLD">

        [2] => <body data-attribute="content" class="HELLO_WORLD">
        [3] => <body class="HELLO_WORLD" id="my-id" data-attribute="content">
    )
于 2011-12-15T01:36:03.073 に答える
0

class="" の背後にあるものが欠落しているため、正規表現を変更する必要があります

/(<ul) ?(([^>]*)class="([^"]*)"([^>]*))?/

以下のテストコード。ul を body タグに置き換えることができます

    <?php
    $pattern = '/(<ul) ?(([^>]*)class="([^"]*)"([^>]*))?/';
    $input_string = '<ul id="test" data-content="the content" class="children" data-compare="equal"><li> test</li></ul>';

    echo preg_replace($pattern, '$1 $3 class="$4 new-class" $5 ', $input_string);

    ?>

画像では、見つかった各変数の内容を確認できます ($1..$5)

クラス名を追加する正規表現の作業

ここで例をテストできますhttps://regex101.com/r/yjQe6G/1

于 2022-02-15T08:48:29.713 に答える