用PHP包装带标题的span的单词
我正在开发一个网站,其中包括一个词汇表,其中解释了一些单词。 另外,我想在一篇文章中描述这些术语,将它们放在带有title属性的跨距中 我从数据库中获取数组中的所有术语:用PHP包装带标题的span的单词,php,replace,words,glossary,Php,Replace,Words,Glossary,我正在开发一个网站,其中包括一个词汇表,其中解释了一些单词。 另外,我想在一篇文章中描述这些术语,将它们放在带有title属性的跨距中 我从数据库中获取数组中的所有术语: $terms = array( 'word1' => 'This is a short description of word1', 'word2' => 'word2 maybe has something to do with word1' ); 包含文本的字符串可以是: $string =
$terms = array(
'word1' => 'This is a short description of word1',
'word2' => 'word2 maybe has something to do with word1'
);
包含文本的字符串可以是:
$string = 'In this sentence I want to explain both, word1 and word2.';
我试图用一个简单的str_ireplace()替换字符串中包含的单词:
foreach($terms作为$term=>$description)
{
$string=str_ireplace($term、.$term.'、$string);
}
现在发生的是,word1和word2被正确替换。但是通过在文本上第二次迭代来搜索和替换word2,它会再次在已经创建的跨度的标题中找到word1
word1的结果如下所示:
<span class="help" title="This is a short description of <span class="help" title="This is a short description of word1">word1</span>word1</span>
您可以使用:
$string = 'In this sentence I want to explain both, word1 and word2.';
$terms = array(
'word1' => '<span title="This is a short description of word1">word1</span>',
'word2' => '<span title="word2 maybe has something to do with word1">word1</span>',
);
echo strtr($string, $terms);
$string='在这句话中,我想同时解释word1和word2';
$terms=数组(
'word1'=>'word1',
'word2'=>'word1',
);
echo strtr($string,$terms);
在这句话中,我想解释单词1和单词1
有关工作示例,请参见
strtr
将翻译字符或替换子字符串您可以使用:
$string = 'In this sentence I want to explain both, word1 and word2.';
$terms = array(
'word1' => '<span title="This is a short description of word1">word1</span>',
'word2' => '<span title="word2 maybe has something to do with word1">word1</span>',
);
echo strtr($string, $terms);
$string='在这句话中,我想同时解释word1和word2';
$terms=数组(
'word1'=>'word1',
'word2'=>'word1',
);
echo strtr($string,$terms);
在这句话中,我想解释单词1和单词1
有关工作示例,请参见
strtr
将翻译字符或替换子字符串哦,伙计,我已经寻找解决方案一周了非常感谢!但是strtr是区分大小写的,不是吗?是否有任何方法或替代方法使其不区分大小写?@chris.ribal如果您查看strtr
手册页面的评论部分,有一些stritr
实现。哦,伙计,我已经寻找解决方案一周了非常感谢!但是strtr是区分大小写的,不是吗?是否有任何方法或替代方法使其不区分大小写?@chris.ribal如果您查看strtr
手册页面的评论部分,会发现一些stritr
实现。