PHP 如何将一个句子的单词存储到一个字符串中而不爆炸
PHP how to store words of a sentence into a string without explode
我正在为我的网站制作一个 php 标签脚本。
我已经完成了代码,但恐怕我的代码太长了。
我的代码可以更简单更简短吗?
<?php
include("admin/apps/site-settings.php"); // database connection
$albumq = mysql_query("select * from albums order by rand() limit 20");
while($album = mysql_fetch_array($albumq)){
$name_a = str_replace("'s","",$album['name']);
$name_b = str_replace(""","",$name_a);
$name_c = str_word_count($name_b, 1);
?>
<?php if(!empty($name_c[0])){?>
<a href="search-<?php echo $name_c[0];?>.html">
<div class="tag">
<?php echo $name_c[0];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[1])){?>
<a href="search-<?php echo $name_c[1];?>.html">
<div class="tag">
<?php echo $name_c[1];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[2])){?>
<a href="search-<?php echo $name_c[2];?>.html">
<div class="tag">
<?php echo $name_c[2];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[3])){?>
<a href="search-<?php echo $name_c[3];?>.html">
<div class="tag">
<?php echo $name_c[3];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[4])){?>
<a href="search-<?php echo $name_c[4];?>.html">
<div class="tag">
<?php echo $name_c[4];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[5])){?>
<a href="search-<?php echo $name_c[5];?>.html">
<div class="tag">
<?php echo $name_c[5];?>
</div>
</a>
<?php }?>
网站输出:
我 运行 它在本地主机上,所以我现在无法提供 link。
使用foreach loop for same , use like
if(count($name_c) > 0)
{
foreach($name_c as $name)
{
?>
<a href="search-<?php echo $name; ?>.html">
<div class="tag">
<?php echo $name; ?>
</div>
</a>
<?php
}
}
<?php
include("admin/apps/site-settings.php"); // database connection
$albumq = mysql_query("select * from albums order by rand() limit 20");
while($album = mysql_fetch_array($albumq)){
$name_a = str_replace("'s","",$album['name']);
$name_b = str_replace(""","",$name_a);
$name_c = str_word_count($name_b, 1);
foreach ($name_c as $value)
{
if (!empty($value))
{
echo "
<a href='search-{$value}.html'>
<div class='tag'>
{$value}
</div>
</a>";
}
}
}
?>
我正在为我的网站制作一个 php 标签脚本。 我已经完成了代码,但恐怕我的代码太长了。
我的代码可以更简单更简短吗?
<?php
include("admin/apps/site-settings.php"); // database connection
$albumq = mysql_query("select * from albums order by rand() limit 20");
while($album = mysql_fetch_array($albumq)){
$name_a = str_replace("'s","",$album['name']);
$name_b = str_replace(""","",$name_a);
$name_c = str_word_count($name_b, 1);
?>
<?php if(!empty($name_c[0])){?>
<a href="search-<?php echo $name_c[0];?>.html">
<div class="tag">
<?php echo $name_c[0];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[1])){?>
<a href="search-<?php echo $name_c[1];?>.html">
<div class="tag">
<?php echo $name_c[1];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[2])){?>
<a href="search-<?php echo $name_c[2];?>.html">
<div class="tag">
<?php echo $name_c[2];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[3])){?>
<a href="search-<?php echo $name_c[3];?>.html">
<div class="tag">
<?php echo $name_c[3];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[4])){?>
<a href="search-<?php echo $name_c[4];?>.html">
<div class="tag">
<?php echo $name_c[4];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[5])){?>
<a href="search-<?php echo $name_c[5];?>.html">
<div class="tag">
<?php echo $name_c[5];?>
</div>
</a>
<?php }?>
网站输出:
我 运行 它在本地主机上,所以我现在无法提供 link。
使用foreach loop for same , use like
if(count($name_c) > 0)
{
foreach($name_c as $name)
{
?>
<a href="search-<?php echo $name; ?>.html">
<div class="tag">
<?php echo $name; ?>
</div>
</a>
<?php
}
}
<?php
include("admin/apps/site-settings.php"); // database connection
$albumq = mysql_query("select * from albums order by rand() limit 20");
while($album = mysql_fetch_array($albumq)){
$name_a = str_replace("'s","",$album['name']);
$name_b = str_replace(""","",$name_a);
$name_c = str_word_count($name_b, 1);
foreach ($name_c as $value)
{
if (!empty($value))
{
echo "
<a href='search-{$value}.html'>
<div class='tag'>
{$value}
</div>
</a>";
}
}
}
?>