简体   繁体   English

使用preg_match和regex显示html表中字符串的内容

[英]Display contents of a string in html table using preg_match and regex

I have a string that contains ASL of users seprated by a pipe 我有一个字符串,其中包含由管道分隔的用户ASL

my string : 我的字符串:

  Jack:22:m:london,uk|john:20:m:otava, canada|ketty:26:f:san fransisco|patricia:19:f:guatemala city

my regex : 我的正则表达式:

 /([A-Za-z]+):([0-9]+):([A-Za-z]):([^|]+/

I want to display these asls on my wepage in html table. 我想在html表格的wepage上显示这些asls。

jack 22 m london,uk
john 20 m otava canada

my code: 我的代码:

    $users="Jack:22:m:london,uk|john:20:m:otava, canada|ketty:26:f:san fransisco|patricia:19:f:guatemala city";

  echo preg_replace('/([A-Za-z]+):([0-9]+):([A-Za-z]):([^|]+)/','<table border="1"><tr><td>$1</td><td>$2</td><td>$3</td><td>$4</td></tr></table>',$users);

It's working as expected but the pipe | 它按预期工作但是管道 is being included in output . 正在纳入产出。

You can see the live demo of my code on https://eval.in/480802 您可以在https://eval.in/480802上看到我的代码的现场演示

Is there something wrong with my regex? 我的正则表达式有问题吗?

When using preg_replace, your regex will not touch the | 使用preg_replace时,你的正则表达式不会触及| character at all, thus simply leaves it there. 角色,因此只是留在那里。 A simple solution is to consume a single | 一个简单的解决方案是使用单个| character if it exists at the end of your current regex pattern: 字符,如果它存在于当前正则表达式模式的末尾:

/([A-Za-z]+):([0-9]+):([A-Za-z]):([^\\|]+)\\|?/ (the \\|? was added at the end) /([A-Za-z]+):([0-9]+):([A-Za-z]):([^\\|]+)\\|?/ (添加\\|?在末尾)

Remember that the | 请记住| character should be escaped too ( \\| ). 字符也应该被转义( \\| )。

If I were you I will simply replace pipes with </td></tr><tr><td> and semi-colons with </td><td> : 如果我是你,我会简单地替换管</td></tr><tr><td>和分号与</td><td>

$users="Jack:22:m:london,uk|john:20:m:otava, canada|ketty:26:f:san fransisco|patricia:19:f:guatemala city";

echo '<table border="1"><tr><td>'
   . strtr($users, ['|'=>'</td></tr><tr><td>', ':'=>'</td><td>'])
   . '</td></tr></table>';

(Note: I don't think you want a different table for each record, so I putted all in the same table with a row for each record). (注意:我认为你不希望每个记录都有一个不同的表,所以我把所有表放在同一个表中,每个记录都有一行)。

If your data only contains | 如果您的数据仅包含| as user separator, and : as user details separator, you can just use 2 explode s like this: 作为用户分隔符,并且:作为用户详细信息分隔符,您可以像这样使用2个explode

$users="Jack:22:m:london,uk|john:20:m:otava, canada|ketty:26:f:san fransisco|patricia:19:f:guatemala city";
$usr_arr = explode('|', $users);
foreach ($usr_arr as $usr)
{
    $parts = explode(':', $usr);
    echo '<table border="1"><tr><td>'.$parts[0].'</td><td>'.$parts[1].'</td><td>'.$parts[2].'</td><td>'.$parts[3].'</td></tr></table>'.PHP_EOL;
}

See IDEONE demo 请参阅IDEONE演示

Output: 输出:

<table border="1"><tr><td>Jack</td><td>22</td><td>m</td><td>london,uk</td></tr></table>
<table border="1"><tr><td>john</td><td>20</td><td>m</td><td>otava, canada</td></tr></table>
<table border="1"><tr><td>ketty</td><td>26</td><td>f</td><td>san fransisco</td></tr></table>
<table border="1"><tr><td>patricia</td><td>19</td><td>f</td><td>guatemala city</td></tr></table>

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

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