在\标签中,将数字后面的空格替换为硬空格。

huangapple go评论79阅读模式
英文:

Replacing space after number with hard space in p tag

问题

I would like to replace a space after a number with hard-space " " only in p tags.

I wrote this code:

   $pattern = "/<p>[0-9] <\/p>/";
   return preg_replace($pattern, "$1&nbsp;", $text);

But it does nothing, why? Can you please help?

Example:

Input : "My 90 days work."
Output: "My 90&nbsp;days work."

Thank you a lot.

英文:

I would like to replace a space after a number with hard-space "&nbsp;" only in p tags.

I wrote this code:

   $pattern = &quot;/&lt;p&gt;[0-9] &lt;\/p&gt;/&quot;;
   return preg_replace($pattern, &quot;$1&amp;nbsp;&quot;, $text);

But it does nothing, why? Can you please help?

Example:

Input : &quot;My 90 days work.&quot;
Output: &quot;My 90&amp;nbsp;days work.&quot;

Thank you a lot.

答案1

得分: 1

$text = '&lt;p&gt;My 90 days 123 work.&lt;/p&gt;';
$pattern = "/&lt;p&gt;(.*?)&lt;\/p&gt;";
$text = preg_replace_callback($pattern, function ($matches) {
    $content = $matches[1];
    $content = preg_replace('/(\d+)\s/', '$1&amp;nbsp;', $content);
    return "&lt;p&gt;{$content}&lt;/p&gt;";
}, $text);
英文:

Try this:

$text = &#39;&lt;p&gt;My 90 days 123 work.&lt;/p&gt;&#39;;
$pattern = &quot;/&lt;p&gt;(.*?)&lt;\/p&gt;/&quot;;
$text = preg_replace_callback($pattern, function ($matches) {
    $content = $matches[1];
    $content = preg_replace(&#39;/(\d+)\s/&#39;, &#39;$1&amp;nbsp;&#39;, $content);
    return &quot;&lt;p&gt;{$content}&lt;/p&gt;&quot;;
}, $text);

答案2

得分: 1

以下是您要翻译的内容:

匹配HTML使用正则表达式并不建议,但如果在两个尖括号之间没有其他字符,您可以使用一个正则模式并使用\G锚点来实现:

(?:&lt;p&gt;(?=[^&lt;&gt;]*&lt;/p&gt;)|\G(?!^))[^\d&lt;&gt;]*\d+\K\h

解释

  • (?: 非捕获组
    • &lt;p&gt;(?=[^&lt;&gt;]*&lt;/p&gt;) 匹配&lt;p&gt;并断言没有尖括号在两者之间
    • | 或者
    • \G(?!^) 断言当前位置在前一个匹配的结尾,但不在字符串的开头
  • ) 关闭非捕获组
  • [^\d&lt;&gt;]* 匹配可选的字符,除了&lt;&gt;或数字
  • \d+ 匹配1个或更多数字
  • \K 忘记之前匹配的内容
  • \h 匹配一个水平空白字符

请参见正则表达式演示PHP演示

$re = '&#39;~(?:&lt;p&gt;(?=[^&lt;&gt;]*&lt;/p&gt;)|\G(?!^))[^\d&lt;&gt;]*\d+\K\h~&#39;';
$str = '&lt;p&gt;My 90 days 123 work.&lt;/p&gt;';

echo preg_replace($re, "&amp;nbsp", $str);

输出

&lt;p&gt;My 90&amp;nbspdays 123&amp;nbspwork.&lt;/p&gt;

或者使用DOMDocument来查找段落,并在匹配到数字和水平空白字符后进行替换:

$str = '&lt;p&gt;My 90 days 123 work.&lt;/p&gt;';
$domDoc = new DOMDocument;
$domDoc->loadHTML($str, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
foreach ($domDoc->getElementsByTagName('p') as $p) {
    echo preg_replace("/\d\K\h/", "&amp;nbsp", $p->nodeValue);
}

输出

My 90&amp;nbspdays 123&amp;nbspwork.
英文:

Matching html with a regex is not advisable, but if there can be no angle brackets in between, you might get away with a single pattern using the \G anchor:

(?:&lt;p&gt;(?=[^&lt;&gt;]*&lt;/p&gt;)|\G(?!^))[^\d&lt;&gt;]*\d+\K\h

Explanation

  • (?: Non capture group
    • &lt;p&gt;(?=[^&lt;&gt;]*&lt;/p&gt;) Match &lt;p&gt; and assert a closing &lt;/p&gt; without angle brackets in between
    • | Or
    • \G(?!^) Assert the current position at the end of the previous match, but not at the start of the string
  • ) Close the non capture group
  • [^\d&lt;&gt;]* Match optional chars other than &lt; or &gt; or a digit
  • \d+ Match 1+ digits
  • \K Forget what is matched so far
  • \h Match a single horizontal whitespace char

See a regex demo and a PHP demo.

$re = &#39;~(?:&lt;p&gt;(?=[^&lt;&gt;]*&lt;/p&gt;)|\G(?!^))[^\d&lt;&gt;]*\d+\K\h~&#39;;
$str = &#39;&lt;p&gt;My 90 days 123 work.&lt;/p&gt;&#39;;

echo preg_replace($re, &quot;&amp;nbsp&quot;, $str);

Output

&lt;p&gt;My 90&amp;nbspdays 123&amp;nbspwork.&lt;/p&gt;

<hr>

Or using DOMDocument to find the paragraphs, and do the replacement after matching a digit and a horizontal whitespace char:

$str = &#39;&lt;p&gt;My 90 days 123 work.&lt;/p&gt;&#39;;
$domDoc = new DOMDocument;
$domDoc-&gt;loadHTML($str, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
foreach ($domDoc-&gt;getElementsByTagName(&#39;p&#39;) as $p) {
    echo preg_replace(&quot;/\d\K\h/&quot;, &quot;&amp;nbsp&quot;, $p-&gt;nodeValue);
}

Output

My 90&amp;nbspdays 123&amp;nbspwork.

huangapple
  • 本文由 发表于 2023年5月29日 17:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定