Forum Moderators: coopster
First of all I think my problem is easy to solve, but me as a PHP noob I can't seem to figure out how to do it.
I got a shoutbox script I've made with some help and it has an excisting URI option. When a URI is entered in the form it will make a link of the posters nick, <a href="http://www.example.net">Poster's nick</a>.
But when nothing is entered the default http:// will be entered into the database making a <a href="http://">Poster's nick</a> link.
So what I want is some sort of easy function that checks if the string "http://" is there in this exact state (so a full URI has to pass this check), if it is, it will replace it with an empty string making no link.
Hope anyone could help me out here.
Celox
First, if there is no entry supplied by the user during registration then I wouldn't even be storing the 'http://' portion of the URI. As a matter of fact I never store the schema anyway because that part can be appended when building the element for display. Less to store, etc. Just something to consider.
However, since we are already at this point I think all you have to do is look for it all by itself in the data value returned and handle accordingly.
if (preg_match('/^http:\/\/$/', trim($subject))) {
// all we have is 'http://' by itself, handle accordingly
} But one minor problem I still have when parsing the data from the shoutbox. When the URI is empty it parses the URI of the page your viewing. For example when blank in the database it shows as <a href="this_is_the_page_i'm_viewing_now.html">Poster's name</a>.
I use the following code for the URI parsing:
$url = $exampe_function['url'];
<a href=\"$url\" target=\"_blank\">$poster</a>
Any ideas?
EDIT: Forgot something, the URI function works when a full URI is entered.
Here's my full shoutview code that doesn't work.
function shoutView(){
$color1 = "#171b2b";
$color2 = "#060606";
$e_shout = mysql_query("SELECT * FROM `shoutbox` ORDER BY id DESC LIMIT 7")
or die(mysql_error());
while ($shoutboxx = mysql_fetch_array($e_shout)) {
$row_color = ($row_count % 2)? $color1 : $color2;
echo "<span style=\"background:$row_color;display:block;width:192px;margin-left:1px;padding:3px;\"><strong>";
$url = $shoutboxx['url'];
$link = "<a";
if ($url) {
$link .= " href=\"$url\" target=\"_blank\"";
}
$link .= ">";
echo "".$shoutboxx['post_by']." said:</a></strong><br />";
echo "".$shoutboxx['content']."</span>";
$row_count++;
}
}
The shoutbox gives no error, but it just doesn't parse links. It only parses:
<strong>Poster said:</a></strong>
Don't I have to echo it in some way?