dongzhuang2030 2015-08-20 13:25
浏览 87
已采纳

使用post传递字符串时正则表达式不起作用

I need to split a email with some rules as explained here: PHP split email with rules

So, I tried the solution on a regex101: https://regex101.com/r/rT0yQ1/1 and it worked, however when I do this on my server with php and pasting the content of email to a textbox and then sending the content by post is doesnt work no more.

Here is my code:

file teste.php:

<form method="post" action="teste2.php" enctype="multipart/form-data">
  <textarea name="email" cols="110" rows="30" id="email"></textarea>
  <br />
  <input type="submit" value="Dividir" />
</form>

file teste2.php:

<?php
    $str = $_POST['email'];
    $re = "/(?:\sF\d+.*?

)(
)/"; 
    $subst = ">>CUT HERE>>"; 
    $result = preg_replace($re, $subst, $str);
    echo $result;
?>

I try this here: https://3v4l.org/mqrJc and it works. However it seems like a problem when I send the text in a textarea by post, because if I use a string it works. Here is the print of the echo:

It prints this:

> A N K U N F T 11.08.15 *** NEUBUCHUNG *** 11.08.15 xxx xxx X3 2830
> 14:25 17:50 18.08.15 xxx xxx X3 2831 18:40 F882129 dsdsaidsaia F882129
> xxxyxyagydaysd A N K U N F T 18.08.15 *** NEUBUCHUNG *** 11.08.15 xxx
> xxx X3 2830 14:25 17:50 18.08.15 xxx xxx X3 2831 18:40 F881554
> ZXCXZCXCXZCCXZ F881554 xcvcxvcxvcvxc F881554 xvcxvcxcvxxvccvxxcv
> 11.08.15 xxx xxx X3 2830 14:25 17:50 18.08.15 xxx xxx X3 2831 18:40 F881605 xczxcdfsfdsdfs F881605 zxccxzxzdffdsfds

It deletes newlines and even worse >>CUT HERE>> does not appear where it is suposed to...

After using nl2br the output is:

A N K U N F T 11.08.15
*** NEUBUCHUNG ***
11.08.15 xxx xxx X3 2830 14:25 17:50
18.08.15 xxx xxx X3 2831 18:40
F882129 dsdsaidsaia
F882129 xxxyxyagydaysd


A N K U N F T 18.08.15
*** NEUBUCHUNG ***
11.08.15 xxx xxx X3 2830 14:25 17:50
18.08.15 xxx xxx X3 2831 18:40
F881554 ZXCXZCXCXZCCXZ
F881554 xcvcxvcxvcvxc
F881554 xvcxvcxcvxxvccvxxcv


11.08.15 xxx xxx X3 2830 14:25 17:50
18.08.15 xxx xxx X3 2831 18:40
F881605 xczxcdfsfdsdfs
F881605 zxccxzxzdffdsfds

Can anyone help me? Thank you very much in advance!

  • 写回答

1条回答 默认 最新

  • dongxuan58311366668 2015-08-20 13:33
    关注

    It's likely that your echo issue is just a simple mistake is displaying the result on a webbrowser. Change the last line from echo $result; to echo nl2br($result);

    nl2br() is a function in php that converts newline characters into <br/> which will show up properly in a webbrowser

    Try the following for the updated regular expression. Your newlines are not , they are so that's all we're changing.

    (?:\sF\d+.*?
    
    )(
    )
    

    3v4l

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路