duandong7980 2011-11-23 18:58
浏览 49
已采纳

3 PHP字符串问题

  1. I have PHP searching in a text file for packages and then it prints back the information. I have a working script but it only works if the user types in the name of the package correctly and uses any captailization that the item has in the file. How can I make PHP less strict about this? Like, a user spelling the word slightly wrong, or forgetting a "-".
  2. Below is another script I'm working on for getting information from a file. My objective is for the script to grab the first line and the third line, not the second. The file contents of the PACKAGES.TXT look something like this:
 PACKAGE NAME:  asdf
 PACKAGE SIZE: 8k
 PACKAGE LOCATION:  www.asdf.com/asdf-package.html

At the moment, I'm using this to gather that information, but it's simply not working with the first and third lines:

$data4 = file('www.asdf.com/PACKAGES.TXT'); //the text file above

for ($i = 0, $found = FALSE; isset($data4[$i]); $i++) { //$i is the counter.
/*$package2 consists of the word searched and a constant string: "PACKAGE NAME:  */
if (trim($data4[$i]) === $package2) { 
$fdas = trim($data3[++$i]); //$fdas is going to hold the url info
   echo ($find2); //$find2 is the original package name.
   echo("<br/>"); //two breaks
   echo("<br/>");
//the lines below handle making a link from a URL and custom package location
$finout = strstr($fdas, '.'); //
$secondfinout = "http://www.asdf.com/".$finout;
echo("<a href='$secondfinout'>$secondfinout</a>");
$found = TRUE;
break; 
}
}

The second step is script I'm writing now. It doesn't work. I do have simpler ones that follow the same concept that do work, the only problem is the strstr($fdas,'.'); puts a dot in the url. I had to start the string somewhere and leave out the parts that are not apart of the url and the dot seems to be my only choice, is there a way I can remove of it?

Thank you so much for any input, I understand this a lengthy question, so feel free to answer what you want of it. I should note that the php script is started by an javascript/html form and then the POSTs are passed to PHP. I am sure those all work right.

Thank you.

  • 写回答

2条回答 默认 最新

  • doubiao7410 2011-11-23 19:06
    关注

    You can enforce case on the comparison:

    if (strtolower(trim($data4[$i])) === strtolower($package2)) { 
    

    or do some real-world enforcement, like running people through a woodchipper if they enter bad-cased data. Workers are plentiful these days and are generally disposable, so seeing fellow data-entry people getting turned into ketchup should promote a general feeling of workplace satisfaction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c