...

View Full Version : Detecting if row contains certain info?



nicky
11-24-2010, 03:27 AM
I have a table that's pulling URLs from a database. The URLs are mostly www.whatever.com, but some are http://subdomain.whatever.com. I need to link the URLs that link obviously, but I'm stumped as to how to put http:// in some and not the other without having to go in and manually edit the rows.

Here's my code:



<td><a href="http://<? echo $rows['url']; ?>"><? echo $rows['url']; ?></a></td>


I thought maybe just an if then statement, but my brain hurts trying to think of one lol

I appreciate any help as there are nearly 1000 rows, and you'd be saving me tons of time! Please and thank you :)

kbluhm
11-24-2010, 04:06 AM
if ( 0 !== strpos( $row['url'], 'http://' ) )
{
$row['url'] = 'http://' . $row['url'];
}

nicky
11-25-2010, 05:19 AM
Omg, you're a lifesaver! Thank you!

nicky
12-09-2010, 02:22 AM
Just realized that some of the URLs contain https:// instead, and some rows contain no URL at all. How might that code snippet be edited to accomodate those rows?

MattF
12-09-2010, 03:04 AM
if (!preg_match('~https?\://~', $row['url']) && !empty($row['url']))
{
$row['url'] = 'http://'.$row['url'];
}

kbluhm
12-09-2010, 03:06 AM
if ( '' != trim( $row['url'] ) && ! preg_match( '/^https?\:\/\//i', $row['url'] ) )
{
$row['url'] = 'http://' . $row['url'];
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum