让我说我有:
$string = '2Pac - Dear Mama';
和db这样:
id - some number
artist_name - 2Pac
alternative_name - Tupac
artist_slug - 2pac
如何将字符串转换为:
<a href="//example.com/artist/2pac/">2Pac</a> - Dear Mama
即使$ string是:
$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 slug与Tupac
(不是2Pac
)名称
答案 0 :(得分:1)
1。 “ - ”可用作分隔符,将字符串分成2部分。字符串的内容放入数组中。 有关爆炸功能的更多信息 - on php.net
$string = 'Tupac - Dear Mama';
$string2 = explode(' - ',$string);
echo "<a href='//example.com/artist/2pac/'>" . $string2[0] . "</a> - " . $string2[1];
2。 我假设你有2张桌子,一张是艺术家,另一张是歌曲。
艺术家:
歌曲:
代码:
$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;");