正则表达式 - PHP正则匹配的移除不规则数量的空格问题

WBOY
Release: 2016-06-06 20:28:10
Original
1289 people have browsed it

<code>$str = "ac   p1     p4     p0 fe   p6     p8   p9 p10 p11 ap7 p3d p12";</code>
Copy after login
Copy after login

写一个正则能匹配出全部 只能由 "p"+数字 的组合

结果是这个样子

<code>p1 p4 p0 p6 p8 p9 p10 p11 p12</code>
Copy after login
Copy after login

该如何写?

回复内容:

<code>$str = "ac   p1     p4     p0 fe   p6     p8   p9 p10 p11 ap7 p3d p12";</code>
Copy after login
Copy after login

写一个正则能匹配出全部 只能由 "p"+数字 的组合

结果是这个样子

<code>p1 p4 p0 p6 p8 p9 p10 p11 p12</code>
Copy after login
Copy after login

该如何写?

<code><?php $str = "ac p1 p4 p0 fe p6 p8 p9 p10 p11 ap7 p3d p12";

preg_match_all('/\bp\d+\b/', $str, $result);

echo json_encode($result[0]);
</code></code>
Copy after login

正则表达式 - PHP正则匹配的移除不规则数量的空格问题

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template