假设我有:
$string = '2Pac - Dear Mama';
数据库是这样的:
id - some number
artist_name - 2Pac
alternative_name - Tupac
artist_slug - 2pac
如何将字符串转换为类似的内容
<a href="//example.com/artist/2pac/">2Pac</a> - Dear Mama
即使$字符串是:
$string = 'Tupac - Dear Mama';
将其转换为:
<a href="//example.com/artist/2pac/">Tupac</a> - Dear Mama
有人能帮我吗?提前谢谢。
//稍后编辑
我已经使用了这个功能,但我想知道如何检查艺术家是否有更多的名字并保持相同的页面
function artist_name($title){ global $site_url;
$words = explode("-", $title); $result = array();
foreach ($words as $word){
$word = trim($word);
$res = mysql_query("SELECT `artist_name` FROM `artists` WHERE `artist_name` = '" . mysql_real_escape_string($word) . "' LIMIT 0, 1;");
if (mysql_num_rows($res) > 0){$result[] = '<a href="'.$site_url.'artist/'.clean_slug_function($word).'/" title="'.$word.'">'.$word.'</a>';}
else{$result[] = $word;}}
$result = implode($result, " - ");
return $result;}
//我是这样做的:
function artist_name($title){ global $site_url;
$words = explode("-", $title); $result = array();
foreach ($words as $word){
$word = trim($word);
$sql = dbquery("SELECT `artist_slug` FROM `artists` WHERE `artist_name` = '".mysql_real_escape_string($word)."' OR `alternative_name` = '".mysql_real_escape_string($word)."' LIMIT 0, 1;");
while($row = dbarray($sql)){$artist_slug = $row['artist_slug'];}
if (mysql_num_rows($sql) > 0){$result[] = '<a href="'.$site_url.'artist/'.clean_slug_function($artist_slug).'/" title="'.$word.'">'.$word.'</a>';}
else{$result[] = $word;}}
$result = implode($result, " - ");
return $result;}
示例:
对于
$title = 'Tupac - Dear Mama';
echo artist_name($title);
结果将是
<a href="http://example.com/artist/2pac/" title="Tupac">Tupac</a> - Dear Mama
*注意2pac
url段塞与Tupac
(而非2Pac
)名称
1。"-"可以用作分隔符,将字符串分成两部分。字符串的内容被放入一个数组中。更多关于爆炸函数的信息-在php.net 上
$string = 'Tupac - Dear Mama';
$string2 = explode(' - ',$string);
echo "<a href='//example.com/artist/2pac/'>" . $string2[0] . "</a> - " . $string2[1];
2。我想你有两张桌子,一张是艺术家的,另一张是歌曲的。
艺术家:
- id
- 艺人名称
- 备选方案名称
- 艺术家
歌曲:
- id
- 艺术家
- 所有权
代码:
$query = "SELECT * FROM artists, songs WHERE songid = 1 AND artists.id = songs.artistid";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
echo "<a href='//example.com/artist/" . $row['artist_name'] . "/'>" . $row['artist_name'] . "</a> - " . $row['title'];
3.
$res = mysql_query("SELECT `artist_name` FROM `artists` WHERE `artist_name` = '" . mysql_real_escape_string($word) . "' OR alternative_name = '" . mysql_real_escape_string($word) . "' LIMIT 0, 1;");