简单 PHP 正则表达式替换
Simple PHP regex replace
给定以下文本:
1. Place pastry on microwave safe plate.2. Heat on high for 3 seconds.3. Cool briefly before handling.
我想替换所有出现的点后跟数字(任何数字)后跟点。
例如
.2. or .15.
并将其替换为
.<BR>number.
对于preg_replace模式,我目前使用的是:
$pattern = "/^(\.[0-9]\.)/";
$replacement = "";
$text= preg_replace($pattern, $replacement, $text);
如何使用 preg_replace 替换文本,以便在第一个点和数字之间放置一个
?
试试这个。这里我们使用 preg_replace
.
Search: /\.(\d+)\./
Added +
for capturing more than one digit and changed capturing group for only digits.
Replace: .<BR>.
</code> will contain digits captured in search expression.</p>
</blockquote>
<p><a href="https://eval.in/860397" rel="nofollow noreferrer"><strong>Try this code snippet here</strong></a></p>
<pre><code><?php
ini_set('display_errors', 1);
$string = "1. Place pastry on microwave safe plate.2. Heat on high for 3 seconds.3. Cool briefly before handling.";
echo preg_replace("/\.(\d+)\./", ".<BR>.", $string);
这将添加号码和新行。
在此处查看演示。 https://regex101.com/r/ktd7TW/1
$re = '/\.(\d+)\./'; //I use () to capture the number and use it in the replace as
$str = '1. Place pastry on microwave safe plate.2. Heat on high for 3 seconds.3. Cool briefly before handling.';
$subst = '.<br>.'; // is the number captured in pattern
$result = preg_replace($re, $subst, $str);
echo $result;
$text = '1. Place pastry on microwave safe plate.2. Heat on high for 3 seconds.3. Cool briefly before handling.';
$pattern = "/(?<=\.)(?=\d+\.)/";
$replacement = "<br>";
$text= preg_replace($pattern, $replacement, $text);
echo $text;
输出:
1. Place pastry on microwave safe plate.<br>2. Heat on high for 3 seconds.<br>3. Cool briefly before handling.
解释:
/ : regex delimiter
(?<=\.) : lookbehind, make sure we have a dot before
(?=\d+\.) : lookahead, make sure we have digits and a dot after
/ : regex delimiter
给定以下文本:
1. Place pastry on microwave safe plate.2. Heat on high for 3 seconds.3. Cool briefly before handling.
我想替换所有出现的点后跟数字(任何数字)后跟点。
例如
.2. or .15.
并将其替换为
.<BR>number.
对于preg_replace模式,我目前使用的是:
$pattern = "/^(\.[0-9]\.)/";
$replacement = "";
$text= preg_replace($pattern, $replacement, $text);
如何使用 preg_replace 替换文本,以便在第一个点和数字之间放置一个
?
试试这个。这里我们使用 preg_replace
.
Search:
/\.(\d+)\./
Added+
for capturing more than one digit and changed capturing group for only digits.Replace:
.<BR>.
</code> will contain digits captured in search expression.</p> </blockquote> <p><a href="https://eval.in/860397" rel="nofollow noreferrer"><strong>Try this code snippet here</strong></a></p> <pre><code><?php ini_set('display_errors', 1); $string = "1. Place pastry on microwave safe plate.2. Heat on high for 3 seconds.3. Cool briefly before handling."; echo preg_replace("/\.(\d+)\./", ".<BR>.", $string);
这将添加号码和新行。
在此处查看演示。 https://regex101.com/r/ktd7TW/1
$re = '/\.(\d+)\./'; //I use () to capture the number and use it in the replace as
$str = '1. Place pastry on microwave safe plate.2. Heat on high for 3 seconds.3. Cool briefly before handling.';
$subst = '.<br>.'; // is the number captured in pattern
$result = preg_replace($re, $subst, $str);
echo $result;
$text = '1. Place pastry on microwave safe plate.2. Heat on high for 3 seconds.3. Cool briefly before handling.';
$pattern = "/(?<=\.)(?=\d+\.)/";
$replacement = "<br>";
$text= preg_replace($pattern, $replacement, $text);
echo $text;
输出:
1. Place pastry on microwave safe plate.<br>2. Heat on high for 3 seconds.<br>3. Cool briefly before handling.
解释:
/ : regex delimiter
(?<=\.) : lookbehind, make sure we have a dot before
(?=\d+\.) : lookahead, make sure we have digits and a dot after
/ : regex delimiter