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

使用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

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题