Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post

    Detecting if row contains certain info?

    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:

    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

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    if ( !== strpos$row['url'], 'http://' ) )
    {
        
    $row['url'] = 'http://' $row['url'];


  • Users who have thanked kbluhm for this post:

    nicky (11-25-2010)

  • #3
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Omg, you're a lifesaver! Thank you!

  • #4
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    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?

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    if (!preg_match('~https?\://~', $row['url']) && !empty($row['url']))
    {
        $row['url'] = 'http://'.$row['url'];
    }

  • Users who have thanked MattF for this post:

    nicky (12-10-2010)

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    if ( '' != trim$row['url'] ) && ! preg_match'/^https?\:\/\//i'$row['url'] ) )
    {
        
    $row['url'] = 'http://' $row['url'];

    Last edited by kbluhm; 12-09-2010 at 10:50 AM.

  • Users who have thanked kbluhm for this post:

    nicky (12-10-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •