Results 1 to 3 of 3

Thread: string manipulation from mysql_fetch_row

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default string manipulation from mysql_fetch_row

    i am trying to knock out the last comma from db record but getting errors
    pretty straight forward, i thought
    Code:
    $query = "SELECT mycell FROM mytable WHERE id='1'";
    $result = mysql_query($query);
    $row = mysql_fetch_row($result);
    $str = $row[0];
    
    //$str = trim( preg_replace(''', "/'/", $str ) );
    $str = substr($str,'',-1);
    
    echo $str . "\n";
    error msg:
    Code:
    Warning: substr() expects parameter 2 to be long, string given in
    and
    Code:
    Warning: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: No ending delimiter '&amp;' found in
    re: this fixed it
    Code:
    $str = substr($str,0,strlen($str)-1);
    re: fixed the last one w/ this
    Code:
    $str = trim( preg_replace('/'/', "'", $str ) );
    Last edited by ggalan; 03-07-2012 at 01:59 AM.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,417
    Thanks
    102
    Thanked 116 Times in 114 Posts

    Default

    If you are going to use PCRE remember to escape your quotes and any other non-alphanumeric character that you want treated as a literal.

    see http://php.net/manual/en/regexp.reference.escape.php

    Depending on your results you may not need to use PCRE here.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,417
    Thanks
    102
    Thanked 116 Times in 114 Posts

    Default

    Here is an example of what it should look like:

    Code:
    <?php 
    $str=" this is jake's test. ";
    $str = trim(preg_replace('/\'/', "\"", $str));
    echo "$str";
    ?>
    I am rather confused on how your posted solution is not producing an error message though.
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

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