2011-12-04 3 views
1

スパンタグ間の文字列に、長さが少なくとも2文字のすべての単語を囲む必要があります。すべての疑問符、句読点などはスパンの外に残す必要があります(a-zとñ、á、éなどの特殊文字も保持する必要があります)。すべての単語をhtmlタグで文字列に囲みますか?

ので、この:任意のアイデア

<a href=http://example.com/prenda>Prenda</a> <a href=http://example.com/de>de</a> <a href=http://example.com/vestir>vestir</a> <a href=http://example.com/que>que</a> 
<a href=http://example.com/se>se</a> <a href=http://example.com/ajusta>ajusta</a>? A <a href=http://example.com/la>la</a> 
<a href=http://example.com/cintura>cintura</a> y <a href=http://example.com/llega>llega</a> 
<a href=http://example.com/generalmente>generalmente</a> <a href=http://example.com/hasta>hasta</a> <a href=http://example.com/el>el</a> <a href=http://example.com/pie>pie</a>. 

Prenda de vestir que se ajusta? A la cintura y llega generalmente hasta el pie. 

はこのことでしょうか?ありがとう!代わりに、この

+0

多分それは役立ちます(http://stackoverflow.com/q/1732348/ 596781)最初に。 –

+0

正規表現を使用して、 – kol

答えて

2

はこれを使用してください。

理由:

" 
\b    # Assert position at a word boundary 
[\p{L}\p{M}] # Match a single character present in the list below 
       # A character with the Unicode property “letter” (any kind of letter from any language) 
       # A character with the Unicode property “mark” (a character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.)) 
    {2,}   # Between 2 and unlimited times, as many times as possible, giving back as needed (greedy) 
\b    # Assert position at a word boundary 
" 

編集:[マッチングオープンタグ]で起動した場合

$result = preg_replace_callback(
     '/\b[\p{L}\p{M}]{2,}\b/u', 
     create_function(
      '$matches', 
      'return <a href=http://example.com/strtolower($matches[0])>$matches[0]</a>;' 
     ), 
     $subject 
); 
+0

がうまくいった!私は例/ $ 0をexample/mb_strtolower($ 0)のようなものに置き換えることができます。 – andufo

+0

@andufoアップデートを確認してください。 – FailedDev

+1

もう一度ありがとう! – andufo

1

用途:

\b(\w{2,})\b 

は基本的には、\bは(句読点を除く、単語の最初と最後にマッチした)「という単語の区切り文字」を意味します。 \wは単語文字ですが、代わりに[a-zA-Z]に置き換えて[0-9_]文字を除外することができます。次に、長さが2+文字を意味する量限定子{2,}を適用します。

代替品ですか?

<a href="http://example.com/$1">$1</a> 

そして、常に高く評価されたexampleです。

+0

Heheを探して、作業コードを書くように練習している間にあなたの解決策で私を殴ってください:) – favoretti

+0

nice :)しかし、特殊文字(ñ)でうまくいきません – andufo

0

を(。anchor tags insteadへの変換例)ここでの例です:

$result = preg_replace('/\b[\p{L}\p{M}]{2,}\b/u', '<a href=http://example.com/$0>$0</a>', $subject); 

すべての文字、すべてのアクセント:

<? 
$without = "Prenda de vestir que se ajusta? A la cintura y llega generalmente hasta el pie."; 
$with = preg_replace("/([A-Za-z]{2,})/", "<a href=\"http://example.com/\\1\">\\1</a>", $without); 
print $with; 
?> 
+0

はうまくいきません特殊文字(ñ、á)で – andufo

関連する問題