0

現在、PHP に操作が必要な文字列があります。

バックエンド コードを変更することはできません。出力を処理することしかできません。

現在、変更したい文字列は、次の形式の一連のリンクです。

<a href="somepage.php">some title</a><a href="somepage2.php">some other title</a><a href="somepage3.php">another title</a>

私が使用しているスクリプトを操作するには、値を増やして各リンクに z-index 値を追加する必要があります。したがって、上記の例では、コードは次のようになる必要があります。

<a href="somepage.php" style="z-index:1">some title</a><a href="somepage2.php" style="z-index:2">some other title</a><a href="somepage3.php" style="z-index:3">another title</a>

str_replace を使用して文字列の一部を置き換える方法を知っているので、すべてのリンクが同じ z-index 値を使用している場合、すべてのケースを検索して<a href置き換えること<a style="z-index:1" hrefができ、問題は解決しますが、各リンクには異なるものが必要ですz インデックス値。

では、複数のリンクを含む文字列を取得し、それぞれに必要な「スタイル」タグと z-index 値を追加する最も効率的な方法は何でしょうか?

編集

また、z-index 値が追加されたら、すべてのリンクを 1 つの文字列に再度結合する必要があることも付け加えておきます。

4

3 に答える 3

2
<?php
$src_str = '<a href="somepage.php">some title</a><a href="somepage2.php">some other title</a><a href="somepage3.php">another title</a>';
$str_list = explode('</a>', $src_str);

$result = '';

$count = 0;
foreach ($str_list as $item)
{
    if (empty($item))
    {
        continue;
    }
    list($part1, $part2) = explode('>', $item);

    $count++;
    $result .= $part1 . " style=\"z-index:$count\">" . $part2 . '</a>';
}
echo $result;

// output:
// <a href="somepage.php" style="z-index:1">some title</a>
// <a href="somepage2.php" style="z-index:2">some other title</a>
// <a href="somepage3.php" style="z-index:3">another title</a>
于 2013-10-31T04:57:00.447 に答える
1
$link = $('a[href]');

$link.each(function(k,v){
 $(v).css('z-index',some_value);
});
于 2013-10-31T04:59:45.863 に答える
0

css を変更するには、jQuery を使用するだけです。そんな感じ :

$(a[href='your-link.php']).css("z-index", "value");
于 2013-10-31T04:53:09.073 に答える