0

これが私の例です:

$content = "
<span class="1">One of </span>the major focuses for <span class = "2">vB 4.1.10 is</span> improving support...";

次のように、タグの外側にすべてのテキストを追加するにはどうすればよいですか。

"<span class="1">One of </span> <span class="3">the major focuses for</span> <span class = "2">vB 4.1.10 is</span> <span class="4">improving support...</span>"
4

2 に答える 2

1
<?php

$content = "abc<span class=\"1\">One of </span>the major focuses for <span class = \"2\">vB 4.1.10 is</span> improving support...";
$result = preg_replace("#>([^<>]+)<([^/]{1,1})#", '><span class="3">$1</span><$2', $content);
$result = preg_replace("#>([^<>]+)$#", '><span class="3">$1</span>', $result);
$result = preg_replace("#^([^<>]+)<([^/]{1,1})#", '<span class="3">$1</span><$2', $result);

var_dump($content);
var_dump($result);

?>
于 2013-01-23T16:11:14.907 に答える
0

これは簡単に改善できますが、基本は次のとおりです。

$content = "<span class=\"1\">One of </span>the major focuses for <span class = \"2\">vB 4.1.10 is</span> improving support...";
$parts = preg_split("!(<span[^>]*>[^>]*<\/span>)!", $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($parts as $key => $val) {
    if (!empty($val)) {
        echo (substr($val, 0, 1) != '<') ? '<span>'.$val.'</span>' : $val;
    }
}
于 2013-01-23T21:44:42.907 に答える