find and replace and update with php and mysql

Discussion in 'PHP' started by jred2002, Sep 23, 2007.

  1. #1
    Well, here's the deal - I had a SQL Server database with about 400,000 records that within one of the columns had urls in them that I wanted to turn into hyperlinks. So I wrote a program in vb that used regular expressions to change them over and one by one insert the records into a mysql database which is now on my server at hostgator(This process took forever). The problem is I was stupid and did not test it enough. It did not replace any of the urls that did not have a www in it. I need to find and replace with php and regular expressions then do an update(the cell is text) on the mysql database. Is this possible? - I normally do everything in ASP.NET and have never written anything in php - I am not asking anyone to write this for me but any help would be greatly appreciated. I hope this made sense.

    Thanks,

    Joe
     
    jred2002, Sep 23, 2007 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Couldn't you just have, in your php:
    echo '<a href="'.$url.'">Anchor Text</a>';
     
    matthewrobertbell, Sep 23, 2007 IP
  3. jred2002

    jred2002 Well-Known Member

    Messages:
    160
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    That would be nice - but unfortunately I am using article dashboard an all the code is encrypted.
     
    jred2002, Sep 23, 2007 IP
  4. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you can validate email using regular expression in mysql
     
    tamilsoft, Sep 23, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    They are URLs... and he knows he can do it with regex, he is asking how to do it practically.

    Something like this?

    mysql_connect('localhost', 'user', 'password');
    mysql_select_db('your_db');
    
    $res = mysql_query("SELECT field FROM table");
    
    while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
      $field = preg_replace('~(^|[^"\'])(http://(www\.)?(.+))(\s|$)~', '<a href="$2">$2</a>', $row[0]);
      mysql_query("UPDATE table SET field = '$field' WHERE field = '$row[0]' ");
    }
    PHP:
     
    krt, Sep 23, 2007 IP
  6. jred2002

    jred2002 Well-Known Member

    Messages:
    160
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    That is what I needed - thank you so much
     
    jred2002, Sep 23, 2007 IP