- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 MySQL 数据库表列,该列具有多种格式的大小值,用户可以在其中手动输入不同的值格式。
当列值与我们创建的模式匹配时,我需要使用 PHP 迭代数据库表并处理该字段以从每一列中获取一个 withd 和 height 值...
以下是这些格式中 90% 的值。许多格式相同,但在小写字母或大写字母 X
Usung PHP 我如何匹配每个字符串以从 X 的左侧和右侧的值中去除所有非数字字符。
左 = 宽度
右侧 = 高度
1x1
1X1
1"x1"
12x12
12X12
12"x12"
12"X12"
NULL
'' ,_ empty field
我只需要将这些值放入 PHP 中的宽度和高度变量中。
如果我可以抓取小写和大写字母 X 广告的左侧以及右侧的所有内容并去除所有非数字,那么我认为它会很容易工作
还有其他值,应该忽略那些值,因为它们不符合模式。下面是我到目前为止发现的一些奇怪值的示例...
18" channel letters
64x20 x 2
Glass Dimensions: 12"x72"
172.61 cm x 28.46 cm
230.15 cm x 42.07 cm
24x24 Interior Double Sided
应该忽略这些类型的值,以便我稍后可以手动编辑它们
最佳答案
我编写了一个名为 rough_strip_all
的函数,它应该去除字符串中列出的所有字符以外的所有字符。添加此步骤可能为您解决问题,但如果没有,您可能需要考虑重新编译以启用对 PCRE 的 UTF8 支持。
<?php
// Strips out all characters except for those in allowed set
function rough_strip_all( $string, $allowed_set = '0123456789x. ' )
{
// Takes the allowed set, splits it into character by character,
// then converts each character in the array to its ASCII value
$allowed_ascii = array_map( function($a) {
return ord( $a );
}, str_split( $allowed_set ) );
$return = '';
for( $i = 0, $ilen = mb_strlen( $string ); $i < $ilen; $i++ )
{
// Check if the ASCII value of current character is in the list of allowed
// ascii characters given. If it is, add it to the return string
$ascii = ord( $string{$i} );
if( in_array( $ascii, $allowed_ascii ) )
{
$return .= $string{$i};
}
}
// Returns the newly compiled string
return $return;
}
// Original string
$string = "Misc text: 35.25”x 21.00” 123 extra text 456";
// Display original string
echo "Original string: {$string}<br />";
// Strips out all characters except the following: '0123456789x. '
$string = rough_strip_all( strtolower( $string ) );
// Strip out all characters except for numbers, letter x, decimal points, and spaces
$string = preg_replace( '/([^0-9x \.])/ui', '', $string );
// Find anything that fits the number X number format (including decimal numbers)
preg_match( '/([0-9]+(\.[0-9]+)?) ?x ?([0-9]+(\.[0-9]+)?)/ui', $string, $values );
// Match found
if( !empty( $values ) )
{
// Set dimensions in easy to read variables
$dimension_a = $values[1];
$dimension_b = $values[3];
// Values returned
echo "Dimension A: {$dimension_a}<br />";
echo "Dimension B: {$dimension_b}<br />";
}
// No match found
else
{
echo "No match found.";
}
?>
这应该也适用于您添加的额外异常值,因为它首先去除所有非必要字符,然后尝试进行匹配。我还向其中添加了一些显示逻辑,这样您就可以看到原始字符串以及处理后的每个维度,或者如果没有匹配项则显示一条消息。
关于php - 使用 PHP for MYSQL 获取字符串左右两侧的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31395149/
我设法检查一个整数是否是从右到左排序的,但我如何检查从左到右的顺序?也许我应该做 2 个功能,一个从左到右检查,另一个从右到左检查?例如,我将 return 0; 替换为第二个函数,该函数检查它是否从
我想在两侧“剪切”我的页面,如下所示: http://i.stack.imgur.com/ngZrp.jpg 演示:https://jsfiddle.net/r2g0eyxf/3/ #left {
我很难在页面的两边都获得背景: 风格 .left { background: url(wax.png); width: 15%; position: absolute; lef
我需要逐步证明:7 + O(n) = O(n) 当两边都有大O的时候,我不知道该怎么做。 我想我理解大 O 符号的概念,但为什么两边都有大 O? 最佳答案 这种表示法的使用不符合大 O 表示法的正式定
我的目标是使 slider 宽度为 1280 像素并位于页面中央。我希望内容的边缘在两侧都有隐藏的溢出。例如,我希望内容的中心始终位于页面的中心,随着浏览器的大小调整,每一边都被切断,并且没有出现水平
我在做什么:在悬停按钮时,我添加了一个 5px 的底部边框。 JS fiddle : http://jsfiddle.net/mUCNB/ 问题: 问题是边框底部在左侧和右侧都延伸了 1px。 问题:
我是一名优秀的程序员,十分优秀!