繁体   English   中英

使用正则表达式php编辑html表

[英]Edit html table using regular expressions php

我被困住了,我希望有人能帮助我。

我正在根据存储在服务器上的文件数据创建表。 我可以根据需要填充表,但是在尝试对文件进行全局更改时遇到麻烦。 当前在表格中有人的名字,但我想这样做,以便我可以单击每个人的名字并将其链接到他们的电子邮件地址。 这是我的代码:

<html>
<head><title>Showing Groups</title></head>
<body>
<?php

  function DisplayRow($target) {
    print "<tr>\n";
    $parts = split(" +", $target);
    for ($i = 0; $i < 10; $i+=1) {
      print "<td>$parts[$i]</td>\n";
    }
    print "<td>\n";
    for ($i = 10; $i < count($parts); $i += 1) {
      print "$parts[$i] ";
    }
    print "</td>\n";
    print "</tr>\n";
  }

  print "<table border=1 width='95%'>\n";

  $allLines = file("cis.txt");
  foreach ($allLines as $oneLine) {     
    if (ereg("^[[:digit:]]", $oneLine)) {
      DisplayRow($oneLine);
    }  
  }
  print "</table>\n";


?>
</body>
</html>

这将产生一个这样的表(但带有表边界):

32133 CIS 100P 004 3.0 MW 1230 1359 CLOU 203 Wong,Jane S

我会在第10列中输入这些名称,以链接到他们的电子邮件地址,就像我上面说的那样。

我正在尝试使用此:

$oneLine=ereg_replace("^[[:upper:]][[:alpha:]]+,[[:blank:]]
[[:upper:]][[:alpha:]]+$", 'x', $oneLine);

正则表达式识别出我关心的是名称,而x只是在使用,因为我试图查看它是否有效。 我还需要知道如何更改每个单独的名称,以使用名字的首字母缩写和姓氏的最多6个字符。

谢谢!

提示:确保张贴所有信息读者可能需要事先知道,例如您的电子邮件地址的格式。现在,在我阅读对您的帖子的评论之前,不清楚从何处获取地址。 有时,当在项目上工作时,您可能会忘记其他人以前从未看过它,而您可能认为某些显而易见的事情对于Stack Overflow读者是未知的。

对于您要执行的操作,可以使用正则表达式,但实际上并非必须如此。 根据您的技能(以及格式化源文件的严格程度),您可能更喜欢使用一些简单的子字符串方法。 顺便说说; 电子邮件地址中的大小写无关紧要。 如果出于美学原因仍然喜欢某种格式,则可以使用strtoupper()strtolower()ucwords()ucfirst() 有关这些方法的更多信息,请参见PHP帮助

一个简单的例子:

<?php
    $name =  "Wong, Jane S"; // you get this from your .txt file
    $name_parts = explode(", ", $name); // Split the string by comma+space
    // Get the first character of the second part of the split string    
    echo substr($name_parts[1], 0, 1);
    // Get the first 6 characters of the last name, or if the name is less than
    // 6 characters long, it will get get the whole name
    echo substr($name_parts[0], 0, 6);
    echo "@example.com"
?>

如果您真的想要一个正则表达式,可以尝试如下操作:

// Get the first 1 to 6 characters in the a-z range that are before the comma
// Then get the first character after the comma and combine them
preg_match('/^([a-zA-Z]{1,6}).+,\s([a-zA-Z])/', $name, $match);
echo $match[2].$match[1]."@example.com";

请注意,在这两个示例中,您可能必须清理电子邮件地址以确保它们仅包含有效字符(例如,电子邮件中不能包含空格,但是某些名称确实使用了它们(例如Van Helsing ))。 如何清理这些内容取决于您使用的格式化系统(清除空白,用下划线/破折号等代替)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM