我无法使用strpos与换行符一起工作 [英] I cannot get strpos to work with newline

查看:166
本文介绍了我无法使用strpos与换行符一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对php很陌生,无法找到解决方法.我有一个表单,想要将一些字符串传递给它,并与另一个文件进行检查.如果所有字符串都在同一行上,则可以正常工作,但是一旦我将多行字符串放入,它将失败.

I am very new to php and cannot find out how to fix this. I have a form and want to pass some string to it and have it checked with another file. It is working fine if all the strings are on the same line but as soon as I put in multiple lines of string, it will fail.

我有一个包含以下代码的php文件:

I have a php file with the following code:

<?php
echo "<center><form method='post' enctype='multipart/form-data'>";
echo "<b></b><textarea name='texttofind' cols='80' rows='10'></textarea><br>";
echo "<input name='submit' type='submit' style='width:80px' value='Run' />";
echo "</form></center>";

$texttofind = $_POST['texttofind'];
if(get_magic_quotes_gpc()) {
    $texttofind = stripslashes($texttofind);
}
$texttofind = html_entity_decode($texttofind);
$s = file_get_contents ("/home/xxxxx/public_html/abc.txt");
if (strpos ($s, $texttofind) === false) {
    echo "not found";
}
else
    echo "found";
?>

在abc.txt中,我有

and in abc.txt, I have

dog  
cat  
rat

无论何时,只要打开php页面并仅输入dog或cat,就可以显示'found'消息,但是当我键入多行(如dog <enter on keyboard> cat)并单击Submit按钮时,它将返回'not found'消息.

Whenever, I open the php page and type in just dog or cat, it will be fine and show 'found' message but when I type multiple lines like 'dog<enter on keyboard>cat' and click submit button, it will return with the 'not found' message.

代码有什么问题,或者无论如何都要对其进行修改以使其能够搜索多行?

What is wrong with the code or anyway to adapt it so that it will be able to search multiple lines?

谢谢.

推荐答案

<?php
$values=explode("\n",$txttofind);
foreach($values as $value)
{
    if (strpos ($s, $value) === false)
    {
        echo "$value : not found <br>";
    }
    else
    {
        echo "$value : found <br>";
    }
}
?>

这篇关于我无法使用strpos与换行符一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆