İlk kelimeyi bulmak Regex

3 Cevap php

Ben sadece bir kelime seçilmiş olur sağlamak için çalışıyorum bu yüzden ancak içerik HTML içeren bir sayfa için içerik ilk sözcüğü bir yayılma eklemek için regex kullanmaya çalışıyorum. Içerik her sayfa için değiştirir.

Şu script:

preg_match('/(<(.*?)>)*/i',$page_content,$matches);
$stripped = substr($page_content,strlen($matches[0]));
preg_match('/\b[a-z]* \b/i',$stripped,$strippedmatch);
echo substr($page_content, 0, strlen($matches[0])).'<span class="h1">'.$strippedmatch[0].'</span>'.substr($stripped, strlen($strippedmatch[0]));

However if the $page_content is <p><span class="title">This is </span> my title!</p> Then my regex thinks the first word is "span" and adds the tags around that.

Bunu düzeltmek için herhangi bir yolu var mı? (Ya da bunu yapmak için daha iyi bir yolu).

3 Cevap

Bu iş gibi görünüyor ...

(?<=\>)\b\w*\b|^\w*\b

Eğer önünde boşluk izin istersen de (sonuç dizesi kırpmaya unutmayın):

(?<=>)\s*\b\w*\b|^\s*\w*\b

If i understand you correct you want a tag around the first word (none tag) with regex you could get that by using this regex

$code = preg_replace('/^(<.+?>\s*)+?(\w+)/i', '\1<span class="h1">\2</span>', $code);

Bu etiketleri dışındaki metni bulana kadar bu sadece etiketleri ve bekler üzerinde döngüler

Bunun için regex kullanarak olmamalı, ama madem ısrar ediyorsun, böyle bir şey deneyebilirsiniz:

<?php

$texts = array(
  '<p><span class="title">This is </span> my title!</p>',
  '<1>   <2>   <3>   blah   blah   <4> <5> blah',
  'garbage <1> <2> real stuff begins <3> <4>',
);

foreach ($texts as $text) {
  print preg_replace('/(>\s*)(\w+)/', '\1{{\2}}', $text, 1)."\n";
}

?>

Bu baskılar:

<p><span class="title">{{This}} is </span> my title!</p>
<1>   <2>   <3>   {{blah}}   blah   <4> <5> blah
garbage <1> <2> {{real}} stuff begins <3> <4>