RegEx用span标签包装每个单词 [英] RegEx wrap every word in a span tag

查看:64
本文介绍了RegEx用span标签包装每个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我试图将每个单词都包装在< span> 标签服务器端。但我错过了正确的RegEx。



我目前的PHP代码看起来像这样。

  function exampleFunction($ text)
{

$ re1 ='/(<.*>)(.*)(<\/.*> )/';
$ str = $ text;
preg_match_all($ re1,$ str,$ matches,PREG_SET_ORDER,0);

$ result = NULL;
foreach($匹配为$ key => $ value){
$ words = $ value [2];
$ start_tag = $ value [1];
$ end_tag = $ value [3];

$ re2 ='/(\w+|.)/';
preg_match_all($ re2,$ words,$ word_matches,PREG_SET_ORDER,0);

$ paragraph = $ start_tag;

foreach($ word_matches as $ key => $ value){
if($ value [0]!=){
$ paragraph。='<跨度>。$值[0]。 '< /跨度>';
} else {
$ paragraph。= $ value [0];
}
}
$段。= $ end_tag;
$ result。= $ paragraph;
}
返回$ result;


替换物不是唯一的问题。



当我放入类似于的元素时

  < h1> Hello,dudes< / h1> 
< p>这个计算器测试文本。使用所有特殊字符如üöä和一些
辛辣ß或?和!你认为一些()或价值约5000美元或
€的是什么? Iam不确定,但这应该够了。并且链接至< a href =google.com> google< / a>会很棒。有关更多文本的内容?那是越野车,
不是吗? üöäßßß< / p>

我得到

 < H1><跨度>你好< /跨度><跨度>,< /跨度> < span> dudes< / span>< / h1>< p>这
stackoverflow测试文本。使用所有特殊字符如üöä和一些辛辣的ß
或?和!你认为一些()或价值大约5000美元或欧元? Iam
不确定,但这应该足够了。并且链接至< a
href =http://google.com> google< / a> <跨度>将< /跨度> <跨度>是< /跨度>
< span>真棒< / span>< span>。< / span> <跨度>请告诉我< /跨度> <跨度>关于< /跨度>
< span>进一步< / span> <跨度>文字及LT; /跨度><跨度>?< /跨度> <跨度>那< /跨度>
< span>越野车< / span>< span>,< / span> <跨度>&未启用LT; /跨度> <跨度>其< /跨度><跨度> ;?
< / span> <跨度>< /跨度><跨度>< /跨度><跨度>< /跨度><跨度>< /跨度>
< span>< / span>< span>< / span> <跨度>?< /跨度><跨度> SSSS< /跨度>< / p为H.

< a> 之后,即我需要什么,除了奇怪的特殊字符。有人可以帮我解决吗?我没有明白这一点。老实说,我认为我只是需要正确的正则表达式来包装每一个字与一个< span> 标记。

解决方案

试试这个正则表达式:

p>

  var str =这个字符串......它是一个例子!< em>字符串< / em>?; 
str.replace(/([A-z0-9'<> /] +)/ g,'< span> $ 1< / span>');

示例:

< pre-class =snippet-code-js lang-js prettyprint-override> var str =This string ... it's,an。example!< em> string< / em>? ; str = str.replace(/([A-z0-9'<> /] +)/ g,'< span> $ 1< / span>'); console.log(str) code>


As the title suggests im trying to wrap every word in a <span> tag server-side. But iam missing the right RegEx.

My current PHP Code looks like this.

 function exampleFunction($text)
{

  $re1 = '/(<.*>)(.*)(<\/.*>)/';
  $str = $text;
  preg_match_all($re1, $str, $matches, PREG_SET_ORDER, 0);

  $result = NULL;
  foreach($matches as $key => $value) {
    $words = $value[2];
    $start_tag = $value[1];
    $end_tag = $value[3];

    $re2 = '/(\w+|.)/';
    preg_match_all($re2, $words, $word_matches, PREG_SET_ORDER, 0);

    $paragraph = $start_tag;

    foreach($word_matches as $key => $value) {
      if($value[0] != " ") {
        $paragraph .= '<span>'.$value[0].'</span>';
      } else {
        $paragraph .= $value[0];
      }
    }
    $paragraph .= $end_tag;
    $result .= $paragraph;
  }
  return $result;

}

The replace thing is not the only problem..

When i put in something like

<h1>Hello, dudes</h1>
<p>This stackoverflow test text. Using all special chars like üöä and some 
spicy ß or ? and !. What do u think about some () or % worth about 5000$ or 
€? Iam not sure, but that should be enough. And a link to <a href="google.com">google</a> would be awesome. Whats about further text? Thats buggy, 
isnt it? üöä ?ßß</p>

I get

<h1><span>Hello</span><span>,</span> <span>dudes</span></h1><p>This 
stackoverflow test text. Using all special chars like üöä and some spicy ß 
or ? and !. What do u think about some () or % worth about 5000$ or €? Iam 
not sure, but that should be enough. And a link to <a 
href="http://google.com">google</a> <span>would</span> <span>be</span> 
<span>awesome</span><span>.</span> <span>Whats</span> <span>about</span> 
<span>further</span> <span>text</span><span>?</span> <span>Thats</span> 
<span>buggy</span><span>,</span> <span>isnt</span> <span>it</span><span>?
</span> <span>�</span><span>�</span><span>�</span><span>�</span>
<span>�</span><span>�</span> <span>?</span><span>ßß</span></p>

After the <a> that is what i need, except the weird special chars. Could somebody please fix that for me? I dont get the point. Honestly i think that i just need the right regex to wrap every word with a <span> tag.

解决方案

Use this code:

Try this regex:

var str = "This string... it's, an. example! <em>string</em>?!";
str.replace(/([A-z0-9'<>/]+)/g, '<span>$1</span>');

Example:

var str = "This string... it's, an. example! <em>string</em>?!";
str = str.replace(/([A-z0-9'<>/]+)/g, '<span>$1</span>');
console.log(str);

这篇关于RegEx用span标签包装每个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆