PHP 从字符串中过滤主题标签并将结果写回文件
PHP filter hashtags from string and write result back to file
我正在使用 fopen()
和 fwrite()
将一些 JSON 内容写入文件。
我的问题:有没有办法过滤内容并只将特定单词写入该文件?
例如:我从 JSON 文件中检索 "I #love #love #love you so #much my dear #brother!"
我想 只写单词 #love
并且只写一次到文件?
这是我在 $message
中得到的示例:
<p> #follow4follow #followme #follow #smile #happy #instalike #instadaily #instagood #life4like #like #likeback #fashion #fun #like4like #sweettooth #spring #gopro #love #tbt</p>
这是我的起点($message
将整个短语写入文件):
$myfile = fopen("custom/hashtag.php", "a");
fwrite($myfile, "<p>" . $message . "</p>" . " \n\r");
/////////////////////////////////////////////
//updated as @insertusernamehere suggested://
/////////////////////////////////////////////
$message = $comment['message']; //i get this from my json
$whitelist = array('#love');
// get only specific hashtag
preg_match_all('/' . implode('|', $whitelist) . '/', $message, $matches);
$unique_matches = array_unique($matches[0]);
$final = implode(' ', $unique_matches);
$myfile = fopen("custom/hashtag.php", "a");
// to avoid empty results
if (!empty($unique_matches)) {
fwrite($myfile, "<p class=\"hidden\">" . $final . "</p>" . " \n\r");
}
纯粹从PHP的角度来看,explode()
你的字符串使用space作为你的分隔符,使用array_unique()
来解决你的重复问题,然后使用一组可接受的单词与使用 array_intersect()
的数组进行比较。将结果写入您的文件。
非常丑陋的代码,不适用于生产,但它可以工作:
<?php
$myallowedwordsarray = array("#love");
$stringtoclean = "I #love #love #love you so much!";
$arraytoclean = explode(" ", $stringtoclean);
$arraytocleanunique = array_unique($arraytoclean);
$cleanedarray = array_intersect($myallowedwordsarray, $arraytocleanunique);
echo $cleantext = implode($cleanedarray, " ");
你可以这样解决:
$message = 'I #love #love #love you so #much!';
使用正则表达式获取所有主题标签
preg_match_all('/#(\w+)/', $message, $matches);
仅获取特定主题标签
这对于 #love
和 #loveYou
.
等类似标签来说是故障保护
$whitelist = array('love', 'Whosebug');
preg_match_all('/#\b(' . implode('|', $whitelist) . ')\b/', $message, $matches);
丢弃重复项
$unique_matches = array_unique($matches[0]);
例如使用空格组合所有主题标签
print implode(' ', $unique_matches);
// prints "#love #much"
或者,如果您想之后通过允许的标签过滤列表
// create a whitelist of hashtags
$whitelist = array('#love', '#Whosebug');
// filter the result by this list
$unique_matches_filtered = array_intersect($whitelist, $unique_matches);
// prints only "#love"
print implode(' ', $unique_matches_filtered);
我正在使用 fopen()
和 fwrite()
将一些 JSON 内容写入文件。
我的问题:有没有办法过滤内容并只将特定单词写入该文件?
例如:我从 JSON 文件中检索 "I #love #love #love you so #much my dear #brother!"
我想 只写单词 #love
并且只写一次到文件?
这是我在 $message
中得到的示例:
<p> #follow4follow #followme #follow #smile #happy #instalike #instadaily #instagood #life4like #like #likeback #fashion #fun #like4like #sweettooth #spring #gopro #love #tbt</p>
这是我的起点($message
将整个短语写入文件):
$myfile = fopen("custom/hashtag.php", "a");
fwrite($myfile, "<p>" . $message . "</p>" . " \n\r");
/////////////////////////////////////////////
//updated as @insertusernamehere suggested://
/////////////////////////////////////////////
$message = $comment['message']; //i get this from my json
$whitelist = array('#love');
// get only specific hashtag
preg_match_all('/' . implode('|', $whitelist) . '/', $message, $matches);
$unique_matches = array_unique($matches[0]);
$final = implode(' ', $unique_matches);
$myfile = fopen("custom/hashtag.php", "a");
// to avoid empty results
if (!empty($unique_matches)) {
fwrite($myfile, "<p class=\"hidden\">" . $final . "</p>" . " \n\r");
}
纯粹从PHP的角度来看,explode()
你的字符串使用space作为你的分隔符,使用array_unique()
来解决你的重复问题,然后使用一组可接受的单词与使用 array_intersect()
的数组进行比较。将结果写入您的文件。
非常丑陋的代码,不适用于生产,但它可以工作:
<?php
$myallowedwordsarray = array("#love");
$stringtoclean = "I #love #love #love you so much!";
$arraytoclean = explode(" ", $stringtoclean);
$arraytocleanunique = array_unique($arraytoclean);
$cleanedarray = array_intersect($myallowedwordsarray, $arraytocleanunique);
echo $cleantext = implode($cleanedarray, " ");
你可以这样解决:
$message = 'I #love #love #love you so #much!';
使用正则表达式获取所有主题标签
preg_match_all('/#(\w+)/', $message, $matches);
仅获取特定主题标签
这对于 #love
和 #loveYou
.
$whitelist = array('love', 'Whosebug');
preg_match_all('/#\b(' . implode('|', $whitelist) . ')\b/', $message, $matches);
丢弃重复项
$unique_matches = array_unique($matches[0]);
例如使用空格组合所有主题标签
print implode(' ', $unique_matches);
// prints "#love #much"
或者,如果您想之后通过允许的标签过滤列表
// create a whitelist of hashtags
$whitelist = array('#love', '#Whosebug');
// filter the result by this list
$unique_matches_filtered = array_intersect($whitelist, $unique_matches);
// prints only "#love"
print implode(' ', $unique_matches_filtered);