我是PHP的新手。我有这种格式的字符串@@##CONTENT-21##@@
,包括页面表的页面描述部分中的内容表中的不同ID。我只需要将id号分开从内容表中调用内容。如何将数字与字符串的特定格式@@##CONTENT-21##@@
分开?
答案 0 :(得分:0)
第一案例
如果您确定所有字符串都以 @@##CONTENT-
开头并以 ##@@
结尾,您可以使用以下代码获取数字,假设$ str是你的字符串,$ id是你存储你的ID的地方。
$id = substr($str,12,strlen($str)-16);
第二个案例
如果你不确定id的位置,但是你确定 ID是字符串中唯一的数字,你可以将它与正则表达式匹配,假设$ str是你的字符串和$ id是您存储ID的地方。
preg_match_all('!\d+!', $con_string, $matches);
$id = implode(' ', $matches[0]);
答案 1 :(得分:0)
$string = '@@##CONTENT-21##@@';
$newnumber = filter_var($string, FILTER_SANITIZE_NUMBER_INT);
//if the number is preceeded by a (-) it will also get the (-) so trim the first char
ltrim($newnumber, '-');