Regex find the first word

Posted by Pez Cuckow on Stack Overflow See other posts from Stack Overflow or by Pez Cuckow
Published on 2010-05-21T08:15:50Z Indexed on 2010/05/21 8:20 UTC
Read the original article Hit count: 321

Filed under:
|

I'm trying to use regex to add a span to the first word of content for a page, however the content contains HTML so I am trying to ensure just a word gets chosen. The content changes for every page.

Current script is:

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.

Is there any way to fix this? (or a better way to do it).

© Stack Overflow or respective owner

Related posts about php

Related posts about regex