Forum Moderators: coopster

Message Too Old, No Replies

Replacing http://

Shoutbox URI function

         

celox

5:16 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



Hey,

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

coopster

5:35 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hi celox, a belated welcome to WebmasterWorld.

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
}

I trim [php.net] the $subject first of any whitespace so if you are certain none exists you could skip that part. Otherwise, the regular expression matches anything that begins and ends with the exact value ... 'http://'

celox

9:26 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



Ok I went for a more database friendly way, just with http:// outside the input field so when left blank it parse nothing into the database table row.

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.

coopster

9:37 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, if the URI from the database column is blank, don't write the href attribute out to the browser.
<?php 
$url = $exampe_function['url'];
$link = "<a";
if ($url) {
$link .= " href=\"$url\" target=\"_blank\"";
}
$link .= ">$poster</a>";
?>

celox

8:20 am on Jan 18, 2007 (gmt 0)

10+ Year Member



For some reason it doesn't work (probably my lack of skills ;))

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?

celox

2:01 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



Got it to work, thanks Coopster for helping me out, couldn't have done it without you.

Celox