Log in

View Full Version : Resolved string manipulation from mysql_fetch_row



ggalan
03-07-2012, 01:31 AM
i am trying to knock out the last comma from db record but getting errors
pretty straight forward, i thought


$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:

Warning: substr() expects parameter 2 to be long, string given in
and

Warning: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: No ending delimiter '&amp;' found in

re: this fixed it

$str = substr($str,0,strlen($str)-1);

re: fixed the last one w/ this

$str = trim( preg_replace('/'/', "'", $str ) );

james438
03-07-2012, 07:03 AM
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 (http://php.net/manual/en/regexp.reference.escape.php)

Depending on your results you may not need to use PCRE here.

james438
03-07-2012, 12:56 PM
Here is an example of what it should look like:


<?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.