PHP - 防止动态创建文本区域中的换行符

时间:2016-10-26 13:40:03

标签: php arrays regex csv line-breaks

我有一个包含可以动态创建的输入集的表单 - see here

为了将从这些输入收集的值推送到.csv表并将它们保存在一个单元格中,我使用以下PHP函数。它从3个输入中收集内容,将其保存到单个单元格中的.csv,并且对于每个新的输入迭代,它只是用" //"分隔条目。但是它保持在同一个单元格中(就像这样:总统,2015年,我是总裁//副总裁,2014年,我是副总裁)。

PHP

$provincialInvolvement = $_POST["provincialInvolvement"];
$provincialInvolvementValues = "";
$e = 0;

foreach($provincialInvolvement as $piValue)
    {
        $provincialInvolvementValues .= $piValue;
        $e++;
        if($e % 3 == 0)
            {
                $provincialInvolvementValues .= "//";
            }

    }

我的问题是因为最后一个输入是textarea,如果放入换行符(有人点击回车键),它会打破.csv的形式并将其放在一个新行上。

我正在尝试将一个函数写入我的PHP部分,因为它将值记录到$provincialInvolvementValues中,它会删除任何换行符,但我没有运气。

这是函数问题,它没有记录任何内容。

$provincialInvolvement = $_POST["provincialInvolvement"];
$provincialInvolvementValues = "";
$e = 0;

foreach($provincialInvolvement as $piValue)
    {
        $provincialInvolvementValues .= $piValue;
        $e++;
        $provincialInvolvementValues = preg_replace( "/(\r|\n)/ ", "", $provincialInvolvementValues ); 
        return $provincialInvolvementValues;
        if($e % 3 == 0)
            {
                $provincialInvolvementValues .= "//";
            }

    }

1 个答案:

答案 0 :(得分:1)

也许试试这个:BoxAndWhisker - 新行通常是import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.GeneralPath; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.text.DecimalFormat; import javax.swing.Icon; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.AxisLocation; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.CategoryItemRendererState; import org.jfree.chart.renderer.category.MinMaxCategoryRenderer; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RectangleInsets; import org.jfree.ui.RefineryUtilities; public class MinMaxCategoryPlotTest extends ApplicationFrame { public MinMaxCategoryPlotTest(String title) { super(title); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(1604, "1", "PESAII"); dataset.addValue(1704, "2", "PESAII"); dataset.addValue(1804, "3", "PESAII"); dataset.addValue(1512, "1", "NSGAII"); dataset.addValue(1612, "2", "NSGAII"); dataset.addValue(1712, "3", "NSGAII"); dataset.addValue(1436, "1", "SPEA2"); dataset.addValue(1536, "2", "SPEA2"); dataset.addValue(1636, "3", "SPEA2"); dataset.addValue(1363, "1", "IBEA"); dataset.addValue(1463, "2", "IBEA"); dataset.addValue(1563, "3", "IBEA"); dataset.addValue(1186, "1", "MOEA/D"); dataset.addValue(1286, "2", "MOEA/D"); dataset.addValue(1386, "3", "MOEA/D"); final CategoryAxis xAxis = new CategoryAxis(""); xAxis.setTickLabelsVisible(false); final NumberAxis yAxis = new NumberAxis("Rating"); yAxis.setRange(1100, 2000); DecimalFormat df = new DecimalFormat("0"); // Override the decimal format to get integer numbers on the axis (1.800 -> 1800) yAxis.setNumberFormatOverride(df); yAxis.setAutoRangeIncludesZero(false); MyMinMaxCategoryRenderer renderer = new MyMinMaxCategoryRenderer(); renderer.setSeriesPaint(0, new Color(0, 0, 0, 0)); // invisible renderer.setSeriesPaint(1, Color.black); renderer.setSeriesPaint(2, new Color(0, 0, 0, 0)); // invisible renderer.setSeriesShape(1, new Line2D.Double(0, -6, 0, 6)); //not working renderer.setSeriesVisible(0, false); // not working renderer.setMinIcon(getIcon(new Line2D.Double(0, -6, 0, 6), true, true)); renderer.setMaxIcon(getIcon(new Line2D.Double(0, -6, 0, 6), true, true)); renderer.setObjectIcon(getIcon(new Line2D.Double(0, -4, 0, 4), true, true)); final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer); plot.setOrientation(PlotOrientation.HORIZONTAL); plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); final JFreeChart chart = new JFreeChart( title, new Font("SansSerif", Font.BOLD, 16), plot, false ); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(1000, 400)); setContentPane(chartPanel); chart.setBackgroundPaint(Color.white); chart.setPadding(new RectangleInsets(10, 10, 10, 10)); // Fix: tick label cut off } private Icon getIcon(Shape shape, final boolean fill, final boolean outline) { final int width = shape.getBounds().width; final int height = shape.getBounds().height; final GeneralPath path = new GeneralPath(shape); return new Icon() { public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g; path.transform(AffineTransform.getTranslateInstance(x, y)); if (fill) { g2.fill(path); } if (outline) { g2.draw(path); } path.transform(AffineTransform.getTranslateInstance(-x, -y)); } public int getIconWidth() { return width; } public int getIconHeight() { return height; } }; } public static void main(String[] args) { MinMaxCategoryPlotTest demo = new MinMaxCategoryPlotTest("Rating Interval"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } public class MyMinMaxCategoryRenderer extends MinMaxCategoryRenderer { @Override public void drawItem(Graphics2D g2, CategoryItemRendererState state, Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis, ValueAxis rangeAxis, CategoryDataset dataset, int row, int column, int pass) { super.drawItem(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column, pass); //Draw label if (dataset.getRowCount() - 1 == row) { //last row Number value = dataset.getValue(row, column); double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(), dataArea, plot.getDomainAxisEdge()); double y1 = rangeAxis.valueToJava2D(value.doubleValue(), dataArea, plot.getRangeAxisEdge()); g2.setFont(new Font("SansSerif", Font.BOLD, 14)); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); String name = dataset.getColumnKey(column).toString(); int width = g2.getFontMetrics().stringWidth(name); g2.drawString(name, (int) y1 - width, (int) x1 - 12); } } } } preg_replace( "\r?\n\s*", "", $provincialInvolvementValues)所以这可能会更好。

此外,\n可能在循环中错误...