我被困住了,我希望有人可以帮助我。
我正在根据存储在服务器上的文件数据创建一个表。我可以像我想的那样填充表格,但是我无法尝试对文件进行全局更改。目前在表中它有人名,但我想这样做,以便我可以点击每个单独的名称并将其链接到他们的电子邮件地址。这是我的代码:
<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个字符的名字。
谢谢!
答案 0 :(得分:0)
提示:请务必事先发布读者可能需要知道的所有信息,例如您的电子邮件地址所在的格式。现在有点不清楚从哪里获取地址,直到我将评论发送给您帖子。有时候在项目工作时你可能会忘记其他人以前从未见过的东西,而且你可能认为明显和清晰的一些东西都不为Stack Overflow读者所知。
对于你想要做的事情,你可以使用正则表达式,但实际上你并不需要。根据您的技能(以及您的源文件格式有多严格),您可能更喜欢使用一些简单的子字符串方法。
顺便说说;资本化在电子邮件地址中并不重要。如果您出于审美原因仍然偏好某种格式,则可以使用strtoupper()
,strtolower()
,ucwords()
和ucfirst()
。有关这些方法的详细信息,请参阅PHP help。
一个简单的例子:
<?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
))。如何清理那些将取决于您正在使用的格式化系统(清除空白,替换为下划线/短划线等)。