我需要将一个字符串拆分成两个相等长度的部分。字符串可以包含空格,逗号或任何内容。我已经引用并尝试了来自链接http://www.testingbrain.com/php-tutorial/php-explode-split-a-string-by-string-into-array.html的explode语句的代码示例,但是它没有显示任何用于分割等长的样本。
还有一件事,虽然分裂的话不应该被打破。
答案 0 :(得分:2)
你走了。
$str="Test string";
$middle=strlen($str)/2;
$first=substr($str,0,$middle);
$last=substr($str,$middle);
答案 1 :(得分:2)
这将在不破坏文字的情况下分割,尽可能在文本的一半,但它可能会分割为任何其他字符(,
,.
,@
等)
$data = "Split a string by length without breaking word"; //string
if (strlen($data) % 2 == 0) //if lenhth is odd number
$length = strlen($data) / 2;
else
$length = (strlen($data) + 1) / 2; //adjust length
for ($i = $length, $j = $length; $i > 0; $i--, $j++) //check towards forward and backward for non-alphabet
{
if (!ctype_alpha($data[$i - 1])) //forward
{
$point = $i; //break point
break;
} else if (!ctype_alpha($data[$j - 1])) //backward
{
$point = $j; //break point
break;
}
}
$string1 = substr($data, 0, $point);
$string2 = substr($data, $point);
答案 2 :(得分:0)
良好的开端,但最初的规范应该稍微改进一点,就像任何奇怪的长度链一样,这是不可能的!这样说,如果你将规格更改为近似相等的长度,那么我将分析半-1字符是否是非字母的,如果这样切割那里,如果它是按字母顺序识别前一个和下一个空格位置并切割最接近一半
答案 3 :(得分:0)
我认为这是一个好的开始:
function calculate() {
/* FLOWERS */
var flowersSize = flsz.options[flsz.selectedIndex].value;
if (flowersSize == 50) {
flpr.value = 175;
flowersPrice = parseFloat(Math.round(175 * 100) / 100).toFixed(2);
flpr.value = "MRP"+" "+flowersPrice;
} else if (flowersSize == 200) {
flpr.value = 599;
flowersPrice = parseFloat(Math.round(599 * 100) / 100).toFixed(2);
flpr.value = "MRP"+" "+flowersPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (flowersSize == 500) {
flpr.value = 1299;
flowersPrice = parseFloat(Math.round(1299 * 100) / 100).toFixed(2);
flpr.value = "MRP"+" "+flowersPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (flowersSize == 1000) {
flpr.value = 2399;
flowersPrice = parseFloat(Math.round(2399 * 100) / 100).toFixed(2);
flpr.value = "MRP"+" "+flowersPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (flowersSize == 5000) {
flpr.value = 10999;
flowersPrice = parseFloat(Math.round(10999 * 100) / 100).toFixed(2);
flpr.value = "MRP"+" "+flowersPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
/* CEREALS */
var cerealsSize = cesz.options[cesz.selectedIndex].value;
if (cerealsSize == 50) {
cepr.value = 199;
cerealsPrice = parseFloat(Math.round(199 * 100) / 100).toFixed(2);
cepr.value = "MRP"+" "+cerealsPrice;
} else if (cerealsSize == 200) {
cepr.value = 699;
cerealsPrice = parseFloat(Math.round(699 * 100) / 100).toFixed(2);
cepr.value = "MRP"+" "+cerealsPrice;
} else if (cerealsSize == 500) {
cepr.value = 1599;
cerealsPrice = parseFloat(Math.round(1599 * 100) / 100).toFixed(2);
cepr.value = "MRP"+" "+cerealsPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (cerealsSize == 1000) {
cepr.value = 2899;
cerealsPrice = parseFloat(Math.round(2899 * 100) / 100).toFixed(2);
cepr.value = "MRP"+" "+cerealsPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (cerealsSize == 5000) {
cepr.value = 12999;
cerealsPrice = parseFloat(Math.round(12999 * 100) / 100).toFixed(2);
cepr.value = "MRP"+" "+cerealsPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
/* VEGETABLES */
var vegetablesSize = vesz.options[vesz.selectedIndex].value;
if (vegetablesSize == 50) {
vepr.value = 225;
vegetablesPrice = parseFloat(Math.round(225 * 100) / 100).toFixed(2);
vepr.value = "MRP"+" "+vegetablesPrice;
} else if (vegetablesSize == 200) {
vepr.value = 799;
vegetablesPrice = parseFloat(Math.round(799 * 100) / 100).toFixed(2);
vepr.value = "MRP"+" "+vegetablesPrice;
} else if (vegetablesSize == 500) {
vepr.value = 1799;
vegetablesPrice = parseFloat(Math.round(1799 * 100) / 100).toFixed(2);
vepr.value = "MRP"+" "+vegetablesPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (vegetablesSize == 1000) {
vepr.value = 3399;
vegetablesPrice = parseFloat(Math.round(3399 * 100) / 100).toFixed(2);
vepr.value = "MRP"+" "+vegetablesPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (vegetablesSize == 5000) {
vepr.value = 14999;
vegetablesPrice = parseFloat(Math.round(14999 * 100) / 100).toFixed(2);
vepr.value = "MRP"+" "+vegetablesPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
/* FRUITS */
var fruitsSize = frsz.options[frsz.selectedIndex].value;
if (fruitsSize == 50) {
frpr.value = 249;
fruitsPrice = parseFloat(Math.round(249 * 100) / 100).toFixed(2);
frpr.value = "MRP"+" "+fruitsPrice;
} else if (fruitsSize == 200) {
frpr.value = 899;
fruitsPrice = parseFloat(Math.round(899 * 100) / 100).toFixed(2);
frpr.value = "MRP"+" "+fruitsPrice;
} else if (fruitsSize == 500) {
frpr.value = 1999;
fruitsPrice = parseFloat(Math.round(1999 * 100) / 100).toFixed(2);
frpr.value = "MRP"+" "+fruitsPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (fruitsSize == 1000) {
frpr.value = 3699;
fruitsPrice = parseFloat(Math.round(3699 * 100) / 100).toFixed(2);
frpr.value = "MRP"+" "+fruitsPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (fruitsSize == 5000) {
frpr.value = 16999;
fruitsPrice = parseFloat(Math.round(16999 * 100) / 100).toFixed(2);
frpr.value = "MRP"+" "+fruitsPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
/* PLANTATIONS */
var plantationsSize = plsz.options[plsz.selectedIndex].value;
if (plantationsSize == 50) {
plpr.value = 275;
plantationsPrice = parseFloat(Math.round(275 * 100) / 100).toFixed(2);
plpr.value = "MRP"+" "+plantationsPrice;
} else if (plantationsSize == 200) {
plpr.value = 999;
plantationsPrice = parseFloat(Math.round(999 * 100) / 100).toFixed(2);
plpr.value = "MRP"+" "+plantationsPrice;
} else if (plantationsSize == 500) {
plpr.value = 2199;
plantationsPrice = parseFloat(Math.round(2199 * 100) / 100).toFixed(2);
plpr.value = "MRP"+" "+plantationsPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (plantationsSize == 1000) {
plpr.value = 3999;
plantationsPrice = parseFloat(Math.round(3999 * 100) / 100).toFixed(2);
plpr.value = "MRP"+" "+plantationsPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else if (plantationsSize == 5000) {
plpr.value = 17999;
plantationsPrice = parseFloat(Math.round(17999 * 100) / 100).toFixed(2);
plpr.value = "MRP"+" "+plantationsPrice.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
var flowersQuantity = flqt.options[flqt.selectedIndex].value;
var cerealsQuantity = ceqt.options[ceqt.selectedIndex].value;
var vegetablesQuantity = veqt.options[veqt.selectedIndex].value;
var fruitsQuantity = frqt.options[frqt.selectedIndex].value;
var plantationsQuantity = plqt.options[plqt.selectedIndex].value;
var flowersLitres = flowersSize * flowersQuantity / 1000;
var cerealsLitres = cerealsSize * cerealsQuantity / 1000;
var vegetablesLitres = vegetablesSize * vegetablesQuantity / 1000;
var fruitsLitres = fruitsSize * fruitsQuantity / 1000;
var plantationsLitres = plantationsSize * plantationsQuantity / 1000;
flowersLitres = parseFloat(Math.round(flowersLitres * 100) / 100).toFixed(3);
cerealsLitres = parseFloat(Math.round(cerealsLitres * 100) / 100).toFixed(3);
vegetablesLitres = parseFloat(Math.round(vegetablesLitres * 100) / 100).toFixed(3);
fruitsLitres = parseFloat(Math.round(fruitsLitres * 100) / 100).toFixed(3);
plantationsLitres = parseFloat(Math.round(plantationsLitres * 100) / 100).toFixed(3);
fllt.value = flowersLitres;
celt.value = cerealsLitres;
velt.value = vegetablesLitres;
frlt.value = fruitsLitres;
pllt.value = plantationsLitres;
var totalAmount = flowersQuantity * flowersPrice + cerealsQuantity * cerealsPrice + vegetablesQuantity * vegetablesPrice + fruitsQuantity * fruitsPrice + plantationsQuantity * plantationsPrice;
totalAmount = parseFloat(Math.round(totalAmount * 100) / 100).toFixed(2);
tamt.value = "MRP"+" "+totalAmount.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}