我能够做到我想要的东西(不使用正则表达式)由:
解析我的字符串
删除所有标签(将它们复制到一个临时数组,并保持一个占位符的每个字符在字符串上)
str_replace新字符串为了替换所有关键字
重新填充它原来的占位符个标签
这是我使用的代码,如果别人需要它:
$str &#61; <<
Moses supposes his toeses are roses,
but Moses supposes erroneously;
for nobody&#39;s toeses are posies of roses,
as Moses supposes his toeses to be.
Ganda Moses!
STRA;
$arr1 &#61; str_split($str);
$arr_links &#61; array();
$phrase_holder &#61; &#39;&#39;;
$current_a &#61; 0;
$goto_arr_links &#61; false;
$close_a &#61; false;
foreach($arr1 as $k &#61;> $v)
{
if ($close_a &#61;&#61; true)
{
if ($v &#61;&#61; &#39;>&#39;) {
$close_a &#61; false;
}
continue;
}
if ($goto_arr_links &#61;&#61; true)
{
$arr_links[$current_a] .&#61; $v;
}
if ($v &#61;&#61; &#39;
// keep collecting every char until
$arr_links[$current_a] .&#61; $v;
$goto_arr_links &#61; true;
} elseif ($v &#61;&#61; &#39;&#39;) { /* */
$arr_links[$current_a] .&#61; "/a>";
$goto_arr_links &#61; false;
$close_a &#61; true;
$phrase_holder .&#61; "{%$current_a%}"; /* put a parameter holder on the phrase */
$current_a&#43;&#43;;
}
elseif ($goto_arr_links &#61;&#61; false) {
$phrase_holder .&#61; $v;
}
}
echo "Links Array:\n";
print_r($arr_links);
echo "\n\n\nPhrase Holder:\n";
echo $phrase_holder;
echo "\n\n\n(pre) Final Phrase (with my keyword replaced):\n";
$final_phrase &#61; str_replace("Moses", "Moses", $phrase_holder);
echo $final_phrase;
echo "\n\n\nFinal Phrase:\n";
foreach($arr_links as $k &#61;> $v)
{
$final_phrase &#61; str_replace("{%$k%}", $v, $final_phrase);
}
echo $final_phrase;
输出&#xff1a;
链接阵&#xff1a;
Array
(
[0] &#61;> Moses
[1] &#61;> Moses
)
短语持有人&#xff1a;
Moses supposes his toeses are roses,
but {%0%} supposes erroneously;
for nobody&#39;s toeses are posies of roses,
as Moses supposes his toeses to be.
Ganda {%1%}!
(预)最后一个短语(与我的关键字替换)&#xff1a;
Moses supposes his toeses are roses,
but {%0%} supposes erroneously;
for nobody&#39;s toeses are posies of roses,
as Moses supposes his toeses to be.
Ganda {%1%}!
最后一个短语&#xff1a;
Moses supposes his toeses are roses,
but Moses supposes erroneously;
for nobody&#39;s toeses are posies of roses,
as Moses supposes his toeses to be.
Ganda Moses!