包含主题标签的字符串替换 - php
String Replace that contains hashtags-php
我有这样的字符串
$content="#Love #Lovestories #Lovewalapyar";
想让这些主题标签可点击。
我有一个数组。
$tags=
(
[0] => stdClass Object
(
[TOPIC_ID] => 132
[TOPIC_NAME] => Love
[TOPIC_URL] => http://local/topic/132/love
)
[1] => stdClass Object
(
[TOPIC_ID] => 3347
[TOPIC_NAME] => LoveStories
[TOPIC_URL] => http://local/topic/3347/lovestories
)
[2] => stdClass Object
(
[TOPIC_ID] => 43447
[TOPIC_NAME] => lovewalapyar
[TOPIC_URL] => http://local/topic/43447/lovewalapyar
)
);
Using this to make hashtags clickable.
foreach($tags as $tag){
$content=str_ireplace('#'.$tag->TOPIC_NAME, '<a href="'.$tag->TOPIC_URL.'" title="'.htmlentities($tag->TOPIC_NAME).'">#'.$tag->TOPIC_NAME.'</a>', $content);
}
得到这个:
它只替换 love 而不是其他字符串。
正在尝试 replace/Make 这些主题标签可点击。
如有任何帮助,我们将不胜感激。
我会使用正则表达式 /#(\w*)/
(主题标签和空格)和 preg_replace()
来替换所有出现的地方。
像这样:
$content = "#Love #Lovestories #Lovewalapyar";
$pattern = '/#(\w*)/';
$replacement = '<a href="#"></a>';
preg_replace($pattern, $replacement, $content);
这会给你:
<a href="#Love">Love</a>
<a href="#Lovestories">Lovestories</a>
<a href="#Lovewalapyar">Lovewalapyar</a>
你可以测试一下regex online here。
如果您需要像评论中提到的 Magnus Eriksson 那样的高级逻辑,您可以使用 preg_match_all
并迭代找到的匹配项。
像这样:
$content = "#Love #Lovestories #Lovewalapyar";
$pattern = '/#(\w*)/';
preg_match_all($pattern, $content, $matches);
foreach ($matches[1] as $key => $match) {
// do whatever you need here, you might want to use $tag = $tags[$key];
}
原因很简单。您的主题标签是其他主题标签的子字符串。
为了避免这种重叠问题,您可以以非递增的方式对数组进行排序,先替换较长的字符串,然后替换较短的字符串,完全避免重叠问题,如下所示:
<?php
usort($tags,function($a,$b){
return strlen($b->TOPIC_NAME) <=> strlen($a->TOPIC_NAME);
});
更新:
您在 <a></a>
中的标签文本导致 str_ireplace
重新考虑它。为此,您需要在数组中传递数组值及其各自的替换项,或者不添加 #
,而是使用 HTML 字符实体 #
,它将被忽略str_ireplace()
并且会正常工作,如下所示:
'<a ...>#'.$tag->TOPIC_NAME.'</a>';
更新的代码段:
<?php
usort($tags,function($a,$b){
return strlen($b->TOPIC_NAME) <=> strlen($a->TOPIC_NAME);
});
foreach($tags as $tag){
$content = str_ireplace('#'.$tag->TOPIC_NAME, '<a href="'.$tag->TOPIC_URL.'" title="'.htmlentities($tag->TOPIC_NAME).'">#'. $tag->TOPIC_NAME.'</a>', $content);
}
我有这样的字符串
$content="#Love #Lovestories #Lovewalapyar";
想让这些主题标签可点击。
我有一个数组。
$tags=
(
[0] => stdClass Object
(
[TOPIC_ID] => 132
[TOPIC_NAME] => Love
[TOPIC_URL] => http://local/topic/132/love
)
[1] => stdClass Object
(
[TOPIC_ID] => 3347
[TOPIC_NAME] => LoveStories
[TOPIC_URL] => http://local/topic/3347/lovestories
)
[2] => stdClass Object
(
[TOPIC_ID] => 43447
[TOPIC_NAME] => lovewalapyar
[TOPIC_URL] => http://local/topic/43447/lovewalapyar
)
);
Using this to make hashtags clickable.
foreach($tags as $tag){
$content=str_ireplace('#'.$tag->TOPIC_NAME, '<a href="'.$tag->TOPIC_URL.'" title="'.htmlentities($tag->TOPIC_NAME).'">#'.$tag->TOPIC_NAME.'</a>', $content);
}
得到这个: 它只替换 love 而不是其他字符串。
正在尝试 replace/Make 这些主题标签可点击。
如有任何帮助,我们将不胜感激。
我会使用正则表达式 /#(\w*)/
(主题标签和空格)和 preg_replace()
来替换所有出现的地方。
像这样:
$content = "#Love #Lovestories #Lovewalapyar";
$pattern = '/#(\w*)/';
$replacement = '<a href="#"></a>';
preg_replace($pattern, $replacement, $content);
这会给你:
<a href="#Love">Love</a>
<a href="#Lovestories">Lovestories</a>
<a href="#Lovewalapyar">Lovewalapyar</a>
你可以测试一下regex online here。
如果您需要像评论中提到的 Magnus Eriksson 那样的高级逻辑,您可以使用 preg_match_all
并迭代找到的匹配项。
像这样:
$content = "#Love #Lovestories #Lovewalapyar";
$pattern = '/#(\w*)/';
preg_match_all($pattern, $content, $matches);
foreach ($matches[1] as $key => $match) {
// do whatever you need here, you might want to use $tag = $tags[$key];
}
原因很简单。您的主题标签是其他主题标签的子字符串。 为了避免这种重叠问题,您可以以非递增的方式对数组进行排序,先替换较长的字符串,然后替换较短的字符串,完全避免重叠问题,如下所示:
<?php
usort($tags,function($a,$b){
return strlen($b->TOPIC_NAME) <=> strlen($a->TOPIC_NAME);
});
更新:
您在 <a></a>
中的标签文本导致 str_ireplace
重新考虑它。为此,您需要在数组中传递数组值及其各自的替换项,或者不添加 #
,而是使用 HTML 字符实体 #
,它将被忽略str_ireplace()
并且会正常工作,如下所示:
'<a ...>#'.$tag->TOPIC_NAME.'</a>';
更新的代码段:
<?php
usort($tags,function($a,$b){
return strlen($b->TOPIC_NAME) <=> strlen($a->TOPIC_NAME);
});
foreach($tags as $tag){
$content = str_ireplace('#'.$tag->TOPIC_NAME, '<a href="'.$tag->TOPIC_URL.'" title="'.htmlentities($tag->TOPIC_NAME).'">#'. $tag->TOPIC_NAME.'</a>', $content);
}