PHP String Replace Question

message from David B on 24 Jul 2004
us-ak, ca-ab

In other words, I need to strip us- and ca- out of $mycode. I have
another script that does just that, but I haven't been able to adapt it
to this one. can someone show me how to write it?

Thanks.

[PHP]
<?php
$result = mysql_query('select count(*) from gatesstatesart');
if (($result) && (mysql_result ($result , 0) > 0)) {
// continue here with the code that starts
//$res = mysql_query ("SELECT * FROM type.....
} else {
die('Invalid query: ' . mysql_error());
}
$res = mysql_query ("SELECT * FROM gatesstatesart
WHERE IDArea = '$mycode'") or die (mysql_error());
while ($row = mysql_fetch_array ($res))

if ($row['GNArt'])
echo $row['GNArt'];
else
include ($_SERVER['DOCUMENT_ROOT']."/includes/const.php");
}
?>
[/PHP]
 
Jason Dalgarno replied to David B on 24 Jul 2004
Zero matching rows is not an invalid query.

$mycode = preg_replace('/^[a-z]{2}-/', '', $mycode);

http://www.php.net/preg_replace
http://www.php.net/pcre.pattern.syntax
 
Michael Fesser replied to David B on 24 Jul 2004
.oO(David B)

substr($mycode, -2)

returns the last two chars.

$res = mysql_query('SELECT ... FROM gatesstatesart
WHERE IDArea = "'.substr($mycode, -2).'"') or ...

Micha
 

Archived message: PHP String Replace Question (Macromedia Dreamweaver)